LeetCode Valid Parentheses
2015-02-08 12:44
176 查看
Given a string containing just the characters
determine if the input string is valid.
The brackets must close in the correct order,
all valid but
not.
题意:括号匹配问题。
思路:利用栈来处理,判断右括号的情况
'(',
')',
'{',
'}',
'['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> st; for (int i = 0; i < s.size(); i++) { if (s[i] == ')' || s[i] == ']' || s[i] == '}') { if (st.empty()) return false; else { char tmp = st.top(); st.pop(); if ((tmp == '(' && s[i] != ')') || (tmp == '[' && s[i] != ']') || (tmp == '{' && s[i] != '}')) return false; } } else st.push(s[i]); } if (!st.empty()) return false; return true; } };
相关文章推荐
- leetcode Longest Valid Parentheses
- [Leetcode] 32. Longest Valid Parentheses
- Leetcode题解 - 20. Valid Parentheses
- leetcode :Valid Parentheses
- LeetCode之20---Valid Parentheses
- leetcode-20-Valid Parentheses
- leetcode Valid Parentheses
- LeetCode(49)-Valid Parentheses
- 【一天一道LeetCode】#20. Valid Parentheses
- [LeetCode] 32. Longest Valid Parentheses
- [LeetCode] Longest Valid Parentheses
- LeetCode 20. Valid Parentheses
- 【Leetcode】Valid Parentheses
- LeetCode Longest Valid Parentheses
- leetcode习题解答:32. Longest Valid Parentheses
- leetcode(20. Valid Parentheses)
- [LeetCode]Valid Parentheses
- [Leetcode] 32. Longest Valid Parentheses
- Leetcode-32. Longest Valid Parentheses
- leetcode -- Valid Parentheses