力扣-栈-(20)

这篇博客持续更新,博主力扣刷题到哪更新到哪。。。希望大家支持

20:有效的括号

  • 在这里插入图片描述
class Solution {
    public boolean isValid(String s) {
        Stack<Character>stack = new Stack<Character>();
        for(char c: s.toCharArray()){
            if(c=='(')stack.push(')');
            else if(c=='[')stack.push(']');
            else if(c=='{')stack.push('}');
            else if(stack.isEmpty()||c!=stack.pop())return false;
        }
        return stack.isEmpty();
    }
}
发布了43 篇原创文章 · 获赞 12 · 访问量 1381

猜你喜欢

转载自blog.csdn.net/qq_42411214/article/details/104326731