LeetCode 20 有效的括号

LeetCode 20 有效的括号,第1张

编程语言:Java
题目链接:https://leetcode-cn.com/problems/valid-parentheses/
题解:水题,这几天没时间,继续做水题
难度:easy
结果:

class Solution {
    static String str = "()[]{}";

    public boolean isValid(String s) {
        Deque<Character> que = new LinkedList<>();
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == '(' || s.charAt(i) == '[' || s.charAt(i) == '{') {
                que.push(s.charAt(i));
            } else {
                if (que.isEmpty()||(str.indexOf(s.charAt(i)) != str.indexOf(que.poll()) + 1)) {
                    return false;
                } 
            }
        }
        return que.isEmpty()?true:false;
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/724016.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-26
下一篇 2022-04-26

发表评论

登录后才能评论

评论列表(0条)

保存