leetcode -- Valid Parentheses
2014-08-13 22:00
302 查看
不要因为走的太远而忘记我们为什么出发
[问题描述]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.
[解题思路]
经典括号匹配问题,使用栈模拟即可
bool Solution::isValid(std::string s) { std::stack<char> tmp; for (int i = 0; i < s.length(); i ++){ if (s[i] == '(' || s[i] == '[' || s[i] == '{') tmp.push(s[i]); else if (s[i] == ')'){ if (tmp.size() > 0 && tmp.top() == '(') tmp.pop(); else return false; } else if (s[i] == ']'){ if (tmp.size() > 0 && tmp.top() == '[') tmp.pop(); else return false; } else if (s[i] == '}'){ if (tmp.size() > 0 && tmp.top() == '{') tmp.pop(); else return false; } } return tmp.size() == 0; }
相关文章推荐
- Leetcode 32. Longest Valid Parentheses (Hard) (cpp)
- leetcode_java.T020_ValidParentheses 给定一个只包含(‘, ‘)’, ‘{‘, ‘}’, ‘[’ 和‘]’的字符串,验证它是否是有效的。
- leetcode--LongestValidParentheses
- leetcode 115: Longest Valid Parentheses
- LeetCode Valid Parentheses Remove Nth Node From End of List
- LeetCode -- Valid Parentheses
- LeetCode——20. Valid Parentheses
- LeetCode20——Valid Parentheses(括号匹配问题,使用栈的知识)
- 49_leetcode_Longest Valid Parentheses
- 【LeetCode20】【Valid Parentheses】
- leetcode20-Valid Parentheses
- leetcode-20 Valid Parentheses
- LeetCode Valid Parentheses
- leetcode - Longest Valid Parentheses
- leetcode 20. Valid Parentheses
- [leetcode] 32. Longest Valid Parentheses
- 【leetcode】20. Valid Parentheses
- Leetcode 20. Valid Parentheses
- [Leetcode] 20 - Valid Parentheses
- [LeetCode]20_Valid Parentheses