Leetcode--Valid Parentheses
来源:互联网 发布:怎么才可以做淘宝模特 编辑:程序博客网 时间:2024/06/16 05:32
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) { int i; vector<char> charVec; stack<char> charStack; //底下的第一个循环有些多余了,直接用stack作就可以少一次for循环 for(i=0;i<s.size();i++) { if(s[i]=='('||s[i]==')'||s[i]=='{'||s[i]=='}'||s[i]=='['||s[i]==']') { charVec.push_back(s[i]); } } if(charVec.size()%2!=0) { return false; } for(i=0;i<charVec.size();i++) { if(charVec[i]=='('||charVec[i]=='{'||charVec[i]=='[') { charStack.push(charVec[i]); } else { if(i==0) { return false; } if(charVec[i]==')') { char tt = charStack.top(); if(tt=='(') { charStack.pop(); } else { return false; } } if(charVec[i]=='}') { char tt = charStack.top(); if(tt=='{') { charStack.pop(); } else { return false; } } if(charVec[i]==']') { char tt = charStack.top(); if(tt=='[') { charStack.pop(); } else { return false; } } } } if(!charStack.empty()) { return false; } return true; } /*bool Match(vector<char>&aa,int one,int two) { if(aa[one]) }*/};
0 0
- LeetCode: Valid Parentheses
- LeetCode: Longest Valid Parentheses
- LeetCode: Valid Parentheses
- LeetCode Longest Valid Parentheses
- [Leetcode] Valid Parentheses
- LeetCode: Longest Valid Parentheses
- [Leetcode] Longest Valid Parentheses
- LeetCode : Valid Parentheses
- [leetcode]longest Valid Parentheses(!!)
- Leetcode: Valid Parentheses
- leetcode 52: valid parentheses
- [Leetcode] Longest Valid Parentheses
- leetcode Longest Valid Parentheses
- 【leetcode】Valid Parentheses
- 【leetcode】Longest Valid Parentheses
- [LeetCode]Longest Valid Parentheses
- [LeetCode]Valid Parentheses
- LeetCode-Valid Parentheses
- POJ 2533 Longest Ordered Subsequence (LIS)
- 树形节点分析方法----城市列表显示
- ListView中item点击事件、item保持选中状态以及其他细节
- HDU 1796 How many integers can you find 容斥原理
- linux删除大文件的前n行
- Leetcode--Valid Parentheses
- OpenVPN协议解析-网络结构之外
- hdu 4910 String && BestCoder Round #3
- 计时器
- Gson解析json
- 最大子矩阵求和 NYOJ 104 && 372 && HDU 1081
- Git入门教程
- MFC学习笔记——static text 和 picture control
- 搜索推广营销五步曲之四 上线实施