[Leetcode]_20 Valid Parentheses
2017-04-26 22:36
387 查看
/** * Index: 20 * Title: Valid Parentheses * Author: ltree98 **/
括号配对,利用栈先进后出的特点。
当出现右半部分时,必须与之前最后出现的左半部分配对成功,否则GG。
注意几个样例:
"[" "]" "][" "[(])" "[()]"
class Solution { public: bool isValid(string s) { stack<char> container; for(int i = 0; i < s.length(); i++) { switch(s[i]) { case '(': case '{': case '[': container.push(s[i]); break; case ')': {if(container.empty() || container.top()!='(') return false; else container.pop();} break; case '}': {if(container.empty() || container.top()!='{') return false; else container.pop();} break; case ']': {if(container.empty() || container.top()!='[') return false; else container.pop();} break; default: ; } } return (container.size() == 0); } };
相关文章推荐
- LeetCode20 Valid Parentheses
- leetcode 20 Valid Parentheses
- python写算法题:leetcode: 20. Valid Parentheses
- 蜗牛慢慢爬 LeetCode 20. Valid Parentheses [Difficulty: Easy]
- 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
- Leetcode - 20. Valid Parentheses
- Leetcode 20. Valid Parentheses
- Leetcode_20_Valid Parentheses
- [LeetCode20]Valid Parentheses
- LeetCode——20. Valid Parentheses
- LeetCode 20: Valid Parentheses