https://leetcode-cn.com/problems/valid-parentheses/
class Solution {
public:
bool isValid(string str) {
stack<int> s;
int n = str.size();
for (int i = 0;i<n; i++)
{
if(str[i]==‘(‘||str[i]==‘[‘||str[i]==‘{‘)
s.push(str[i]);
else if(str[i]==‘)‘)
{
if(s.empty())
return 0;
if(s.top()==‘(‘)
s.pop();
else
return 0;
}
else if(str[i]==‘]‘)
{
if(s.empty())
return 0;
if(s.top()==‘[‘)
s.pop();
else
return 0;
}
else if(str[i]==‘}‘)
{
if(s.empty())
return 0;
if(s.top()==‘{‘)
s.pop();
else
return 0;
}
}
if(!s.empty())
return 0;
else
return 1;
}
};
用leetcode提交的C++代码,其中字符串s的长度使用:s.size()
用codeblocks编译:strlen(s)
原文:https://www.cnblogs.com/jiaxinwei/p/12748201.html