Valid Parentheses(括号匹配)

解决思路:

1. 栈

2.使用Map,判断是否

 public boolean isValid(String s) {
        Stack<Character> parens = new Stack<>();

        Map<Character, Character> parenMap = new HashMap<>();
        parenMap.put(‘(‘, ‘)‘);
        parenMap.put(‘[‘, ‘]‘);
        parenMap.put(‘{‘, ‘}‘);

        for (int i = 0; i < s.length(); i++) {
            char paren = s.charAt(i);
            boolean isLeft = parenMap.containsKey(paren);

            if (isLeft) {
                parens.push(paren);
            } else {
                if (parens.empty()) {
                    return false;
                }

                char leftSide = parens.pop();
                if (parenMap.get(leftSide) != paren) {
                    return false;
                }

            }
        }

        return parens.empty();
    }

匹配

原文地址:https://www.cnblogs.com/da-peng/p/8278050.html

时间: 2024-11-10 14:08:21

Valid Parentheses(括号匹配)的相关文章

20. Valid Parentheses括号匹配

20 Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]"

LeetCode 20 Valid Parentheses 括号匹配问题

题目: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]&

20. Valid Parentheses(括号匹配,用桟)

Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]"

leetcode 20 Valid Parentheses 括号匹配

Given a string containing just the characters '(', ')', '{', '}', '[' and']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]"

20. Valid Parentheses - 括号匹配验证

Description: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. Example: The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]&quo

Valid Parentheses——括号匹配算法

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/41450987 通过本文你能学到如下知识: (1)对数据结构中栈的理解,特别是Stack类中的peek()方法和pop()方法的区别. (2)理解解题思路,提高思考问题的能力. Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if

20/32/22/856/301/921 Parentheses 括号匹配或者生成题

20. https://leetcode.com/problems/valid-parentheses/description/ 32. https://leetcode.com/problems/longest-valid-parentheses/description/ 22. https://leetcode.com/problems/generate-parentheses/description/ 856. https://leetcode.com/problems/score-of-

leetcode题解:Valid Parentheses(栈的应用-括号匹配)

题目: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]&

Leetcode: Valid Parentheses 有效的括号匹配

Valid Parentheses: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]"