zoukankan      html  css  js  c++  java
  • 【leetcode】20. Valid Parentheses

    检查括号对是否匹配

    这题需要注意:

    1. 栈的空指针
    2. 全局变量stack需要clear
    3. 输入结束时栈长度不为空
    public class Solution {
        private static final Stack<Character> stack = new Stack<Character>();
        
        public boolean isValid(String s) {
            stack.clear();
            for (int i = 0; i < s.length(); ++i) {
                char c = s.charAt(i);
                if (!isLeft(c)) {
                    if (stack.isEmpty()) {
                        return false;
                    }
                    if (!isPair(stack.pop(), c)) {
                        return false;
                    }
                } else {
                    stack.push(c);
                }
            }
            return stack.isEmpty();
        }
        
        private boolean isLeft(char c) {
            return c == '(' || c == '[' || c == '{';
        }
        
        private boolean isPair(char left, char right) {
            if (left == '(') {
                return right == ')';
            } else if (left == '[') {
                return right == ']';
            } else {
                return right == '}';
            }
        }
    }
  • 相关阅读:
    文件搜索和图像裁剪
    Mat的复制
    map
    substr
    cin,scanf
    strstr
    Applying vector median filter on RGB image based on matlab
    sobel算子的一些细节
    matlab 有趣小细节
    高斯混合模型(GMM)
  • 原文地址:https://www.cnblogs.com/lanhj/p/5372713.html
Copyright © 2011-2022 走看看