有效的括号序列
给定一个字符串所表示的括号序列,包含以下字符: ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[’ and ‘]’, 判定是否是有效的括号序列。
class Solution {
public:
/**
* @param s A string
* @return whether the string is a valid parentheses
*/
bool isValidParentheses(string& s) {
// Write your code here
int len = s.length();
int i=0;
stack<char> st;
for(;i<len;i++)
{
if(s[i] == '(' || s[i] == '[' || s[i]=='{')
st.push(s[i]);
else
{
if(st.top() == '(' && s[i] == ')')
{
st.pop();
}
else if(s[i] == ']' && st.top()=='[')
{
st.pop();
}
else if(s[i] == '}' && st.top() == '{')
{
st.pop();
}else{
return false;
}
}
}
if(st.empty())
return true;
return false;
}
};