[leetcode] 20. 有效的括号

20. 有效的括号

括号匹配,用栈即可。高中时第一次学栈后做的第一个题。。

class Solution {
    public boolean isValid(String s) {
        Stack<Character> characterStack = new Stack<>();

        for (int i = 0; i < s.length(); i++) {
            switch (s.charAt(i)) {
                case ‘(‘:
                case ‘[‘:
                case ‘{‘:
                    characterStack.push(s.charAt(i));
                    break;
                case ‘)‘:
                    if (!characterStack.isEmpty() && characterStack.peek() == ‘(‘) {
                        characterStack.pop();
                        break;
                    } else {
                        return false;
                    }
                case ‘]‘:
                    if (!characterStack.isEmpty() && characterStack.peek() == ‘[‘) {
                        characterStack.pop();
                        break;
                    } else {
                        return false;
                    }
                case ‘}‘:
                    if (!characterStack.isEmpty() && characterStack.peek() == ‘{‘) {
                        characterStack.pop();
                        break;
                    } else {
                        return false;
                    }
            }
        }

        return characterStack.isEmpty();
    }
}

原文地址:https://www.cnblogs.com/acbingo/p/9250790.html

时间: 2024-10-29 18:03:41

[leetcode] 20. 有效的括号的相关文章

Leetcode 20.有效的括号 js

---恢复内容开始--- 题目描述: 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效.有效字符串需满足: 左括号必须用相同类型的右括号闭合. 左括号必须以正确的顺序闭合. 注意空字符串可被认为是有效字符串. 示例 1: 输入: "()"输出: true 示例 2: 输入: "()[]{}"输出: true 示例 3: 输入: "(]"输出: false 示例 4: 输入: "([)]&quo

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 "([)]&

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 "([)]"

leetcode——20. 有效的括号

简单题: 用栈实现: class Solution: def isValid(self, s: str) -> bool: if s=='': return True if len(s)==1: return False stack=[] i=0 while i<len(s): if s[i] in '({[': stack.append(s[i]) i+=1 if i==len(s): return False else: if stack==[]: return False if (s[i

LeetCode:有效的括号【20】

LeetCode:有效的括号[20] 题目描述 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合. 左括号必须以正确的顺序闭合. 注意空字符串可被认为是有效字符串. 示例 1: 输入: "()" 输出: true 示例 2: 输入: "()[]{}" 输出: true 示例 3: 输入: "(]" 输出: false 示例 4: 输入: "

[LeetCode]20 Valid Parentheses 有效的括号

[LeetCode]20 Valid Parentheses 有效的括号 Description Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input string is valid if: Open brackets must be closed by the same type of brac

leetcode 20 简单括号匹配

栈的运用 1 class Solution { 2 public: 3 bool isValid(string s) { 4 stack<char>The_Stack; 5 int i=0; 6 The_Stack.push('#'); 7 while(i<s.size()) { 8 if((s[i]==')'&&The_Stack.top()=='(')||(s[i]==']'&&The_Stack.top()=='[')||(s[i]=='}'&

leetCode 20.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 "(]" a

Leetcode题目20.有效的括号(简单)

题目描述: 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合.左括号必须以正确的顺序闭合.注意空字符串可被认为是有效字符串. 示例 1: 输入: "()"输出: true示例 2: 输入: "()[]{}"输出: true示例 3: 输入: "(]"输出: false示例 4: 输入: "([)]"输出: false示例 5: