20. Valid Parentheses
2016-09-11 20:11
183 查看
使用栈
class Solution {
public:
bool isValid(string s) {
stack<char> st;
int n=s.size();
int i;
for(i=0;i<n;i++)
{
if(s[i]==')')
{
if(!st.empty()&&st.top()=='(')
st.pop();
else
return false;
}
else if(s[i]==']')
{
if(!st.empty()&&st.top()=='[')
st.pop();
else
return false;
}
else if(s[i]=='}')
{
if(!st.empty()&&st.top()=='{')
st.pop();
else
return false;
}
else
st.push(s[i]);
}
if(st.empty())
return true;
return false;
}
};
class Solution {
public:
bool isValid(string s) {
stack<char> st;
int n=s.size();
int i;
for(i=0;i<n;i++)
{
if(s[i]==')')
{
if(!st.empty()&&st.top()=='(')
st.pop();
else
return false;
}
else if(s[i]==']')
{
if(!st.empty()&&st.top()=='[')
st.pop();
else
return false;
}
else if(s[i]=='}')
{
if(!st.empty()&&st.top()=='{')
st.pop();
else
return false;
}
else
st.push(s[i]);
}
if(st.empty())
return true;
return false;
}
};
相关文章推荐
- 20. Valid Parentheses
- leetcode 20. Valid Parentheses
- 《LeetBook》leetcode题解(20):Valid Parentheses[E]——栈解决括号匹配问题
- 【leetcode】20. Valid Parentheses
- [LeetCode-Algorithms-20] "Valid Parentheses" (2017.11.1-WEEK9)
- 20. Valid Parentheses
- [LeetCode]20. Valid Parentheses
- leetcode 20: Valid Parentheses
- [Leetcode]20. Valid Parentheses
- [LeetCode]20_Valid Parentheses
- LeetCode (20)Valid Parentheses
- 20. Valid Parentheses
- Leetcode 20. Valid Parentheses - javascript
- [leetcode] 20. Valid Parentheses
- 20. Valid Parentheses
- leetcode解题报告20. Valid Parentheses
- 20. Valid Parentheses题解
- Leetcode-20. Valid Parentheses
- 20. Valid Parentheses
- Leetcode 20. Valid Parentheses