leetcode 20. Valid Parentheses
2017-06-25 12:26
337 查看
Given a string containing just the characters
The brackets must close in the correct order,
解:这是一道简单题,理解算数式子的实现就是利用栈
class Solution {
public:
bool isValid(string s) {
stack<char> tmp;
for(int i = 0; i < s.size(); ++i){
switch(s[i]){
case '(':
case '[':
case '{': tmp.push(s[i]);break;
case ')': if(tmp.empty() || tmp.top() != '(') return false;else tmp.pop();break;
case ']': if(tmp.empty() || tmp.top() != '[') return false;else tmp.pop();break;
case '}': if(tmp.empty() || tmp.top() != '{') return false;else tmp.pop();break;
default: ;
}
}
return tmp.empty();
}
};
'(',
')',
'{',
'}',
'['and
']', determine if the input string is valid.
The brackets must close in the correct order,
"()"and
"()[]{}"are all valid but
"(]"and
"([)]"are not.
解:这是一道简单题,理解算数式子的实现就是利用栈
class Solution {
public:
bool isValid(string s) {
stack<char> tmp;
for(int i = 0; i < s.size(); ++i){
switch(s[i]){
case '(':
case '[':
case '{': tmp.push(s[i]);break;
case ')': if(tmp.empty() || tmp.top() != '(') return false;else tmp.pop();break;
case ']': if(tmp.empty() || tmp.top() != '[') return false;else tmp.pop();break;
case '}': if(tmp.empty() || tmp.top() != '{') return false;else tmp.pop();break;
default: ;
}
}
return tmp.empty();
}
};
相关文章推荐
- LeetCode 20. Valid Parentheses
- leetcode 20 Valid Parentheses 括号匹配
- LeetCode20 Valid Parentheses
- leetcode 20. Valid Parentheses
- leetcode:20 Valid Parentheses-每日编程第三十七题
- leetcode——20——Valid Parentheses
- [LeetCode]--20. Valid Parentheses
- [leetcode:python]20.Valid Parentheses
- LeetCode 20 - Valid Parentheses
- LeetCode 20. Valid Parentheses(合法括号序列)
- 小白笔记--------------------------leetcode(20. Valid Parentheses )
- leetcode-20-Valid Parentheses
- LeetCode 20. Valid Parentheses
- LeetCode 20. Valid Parentheses
- LeetCode 20 Valid Parentheses (括号匹配问题)
- LeetCode 20. Valid Parentheses--验证括号是否有效
- [leetcode] 20.Valid Parentheses
- leetcode 20. Valid Parentheses
- [C++]LeetCode 20: Valid Parentheses(判断运算符有效性)
- [LeetCode] 20. Valid Parentheses