https://leetcode-cn.com/problems/valid-parentheses/description/
class Solution
{
public:
bool isValid(string s)
{
stack<char> ss;
//char stack[1000];
//int top = 0;
int len = s.length();
int flag = 1;
int i = 0;
while(i<len && flag)
{
if(s[i] == '(' || s[i] == '[' || s[i] == '{')
ss.push(s[i]);
if(s[i] == ')')
if(!ss.empty() && ss.top() == '(')
ss.pop();
else
flag = 0;
if(s[i] == ']')
if(!ss.empty() && ss.top() == '[')
ss.pop();
else
flag = 0;
if(s[i] == '}')
if(!ss.empty() && ss.top() == '{')
ss.pop();
else
flag = 0;
i++;
}
if(ss.empty() && flag == 1)
return true;
else
return false;
}
};
因为有pop()操作,所以需要先判断栈是否空