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

题解: 典型的STL stack 应用。注意边界条件。

 1 class Solution {
 2 public:
 3     bool isValid(string s) {
 4         stack<char> st;
 5         if(s.size()<=1 || s.size()%2) return false;
 6
 7         for(int i=0;i<s.size();i++)
 8         {
 9             if(s[i]==‘(‘ || s[i]==‘[‘ || s[i]==‘{‘) st.push(s[i]);
10             else if(st.empty())
11             {
12                 return false;
13             }
14             else
15             {
16                 if(  (s[i]==‘)‘ && st.top()==‘(‘) ||
17                      (s[i]==‘]‘ && st.top()==‘[‘) ||
18                      (s[i]==‘}‘ && st.top()==‘{‘) )
19                      st.pop();
20                 else
21                     return false;
22             }
23         }
24         if(!st.empty()) return false;
25         else return true;
26     }
27 };

转载请注明出处: http://www.cnblogs.com/double-win/ 谢谢!

LeetCode: Valid Parentheses 题解

时间: 2025-01-13 07:32:03

LeetCode: Valid Parentheses 题解的相关文章

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

LeetCode: Valid Parentheses [020]

[题目] 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

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: Generate Parentheses 题解

Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "()()()" 题解: 很容易

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

[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"([)]"are not. 题意:给

[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 Remove Nth Node From End of List

public ListNode removeNthFromEnd(ListNode head, int n) { if(head==null) return null; if(n==0)//如果n==0 return head; Map<Integer,ListNode> map=new HashMap<Integer, ListNode>(); int i=0; ListNode temp=head; do { map.put(++i, temp); temp=temp.next

leetcode Valid Parentheses python

# 解题思路: # 创建一个字典映射关系 dicts# 使用一个栈stk 遍历字符串s 得到一个新的字符串curItem 如果lastItem在dicts中的value和它相等 不做任何操作# 如果不等 入栈 有lastItem的 先append lastItem 然后是curItem ## 最后判断如果stk为空说明所给字符串匹配 return true class Solution(object): def isValid(self, s): """ :type s: s