leetCode 20. Valid Parentheses 字符串
2016-08-11 00:30
399 查看
20. Valid Parentheses
Given a string containing just the characters
括号匹配问题。
思路:
采用压栈出栈来处理。
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) { if(s.size() == 0 || s.size() % 2 == 1) return false; map<char, char> brackets; brackets.insert(pair<char, char>('}', '{')); brackets.insert(pair<char, char>(']', '[')); brackets.insert(pair<char, char>(')', '(')); stack<char> mystack; for(int i = 0 ;i < s.size();i++) { if(s[i] == '{' || s[i] == '[' || s[i] == '(' ) { mystack.push(s[i]); } else { if( mystack.size() < 1) return false; if(brackets[s[i]] == mystack.top()) mystack.pop(); else return false; } } if(mystack.size() == 0) return true; else return false; } };2016-08-11 00:30:15
相关文章推荐
- [LeetCode 20] Valid Parentheses Solution
- leetcode20-Valid Parentheses
- LeetCode 20.Valid Parentheses
- [Leetcode 20] 20 Valid Parentheses
- leetcode 20 Valid Parentheses
- LeetCode 20 — Valid Parentheses(C++ Java Python)
- 【leetcode】20—valid parentheses
- 20. Valid Parentheses Leetcode Python
- 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]20 Valid Parentheses
- [Leetcode 20, Easy] Valid Parentheses
- leetcode-20 Valid Parentheses
- LeetCode --- 20. Valid Parentheses
- [Leetcode] 20. Valid Parentheses