20 valid parentheses

输入:s = “(]”
输出:false

class Solution {
    public boolean isValid(String s) {
        //栈
        Stack<Character> stack = new Stack();
        int len = s.length();
 
        for(char c:s.toCharArray())
        {
            //为左边的就加入右边的
            if(c == '(')
                stack.push(')');
            else if(c == '{')
                stack.push('}');
            else if(c == '[')
                stack.push(']');
            else 
            	//栈为空或不匹配就false
                if(stack.empty() || stack.pop()!=c)
                    return false;
        }
 
        return stack.empty();
    }
}
 
上一篇:idea创建Maven项目时Maven插件内看不到mybatis-generator


下一篇:[LeetCode] 1111. Maximum Nesting Depth of Two Valid Parentheses Strings 有效括号的嵌套深度