LeetCode之Valid Parentheses
2014-03-31 15:16
148 查看
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.
class Solution {
public:
bool isValid(string s) {
stack<char> cstack;
for (int i = 0; i<s.length(); i++){
switch (s[i]){
case '(':
cstack.push('(');
break;
case '[':
cstack.push('[');
break;
case '{':
cstack.push('{');
break;
case ')':
if (!cstack.empty() && cstack.top()=='(')
cstack.pop();
else return false;
break;
case ']':
if (!cstack.empty() && cstack.top() == '[')
cstack.pop();
else return false;
break;
case '}':
if (!cstack.empty() && cstack.top() == '{')
cstack.pop();
else return false;
break;
default:
return false;break;
}//switch
}//for
if (cstack.empty())
return true;
return false;
}
};
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> cstack;
for (int i = 0; i<s.length(); i++){
switch (s[i]){
case '(':
cstack.push('(');
break;
case '[':
cstack.push('[');
break;
case '{':
cstack.push('{');
break;
case ')':
if (!cstack.empty() && cstack.top()=='(')
cstack.pop();
else return false;
break;
case ']':
if (!cstack.empty() && cstack.top() == '[')
cstack.pop();
else return false;
break;
case '}':
if (!cstack.empty() && cstack.top() == '{')
cstack.pop();
else return false;
break;
default:
return false;break;
}//switch
}//for
if (cstack.empty())
return true;
return false;
}
};
相关文章推荐
- LeetCode 32. Longest Valid Parentheses
- [LeetCode]20. Valid Parentheses
- Leetcode 20: Valid Parentheses
- Longest Valid Parentheses @Leetcode -- Python
- [LeetCode] Valid Parentheses
- [LeetCode]Longest Valid Parentheses, 解题报告
- leetcode Longest Valid Parentheses
- 【leetcode】20. Valid Parentheses
- leetcode 日经贴,python code -Valid Parentheses
- LeetCode Valid Parentheses
- Python学习——leetcode(Valid Parentheses)
- [leetcode]_Valid Parentheses
- [LeetCode] Valid Parentheses 验证括号
- leetcode做题总结,题目Longest Valid Parentheses 2012/02/29
- LeetCode Valid Parentheses
- leetcode-longest valid parentheses
- leetcode--20. Valid Parentheses
- leetcode_20题——Valid Parentheses(string,stack堆栈)
- Leetcode 32. Longest Valid Parentheses
- leetcode第20题——*Valid Parentheses