#include<iostream>
#include<stack>
#include<string>
using namespace std;
class Solution {
public:
bool isValid(string s) {
int l=s.size();
stack<char> stk;
int j=0,i=0;
char c,d;
for(i=0;i<l;i++)
{
if(s[i]=='('||s[i]=='{'||s[i]=='[')
{
stk.push(s[i]);
}
else
{
c=stk.top();
switch(s[i])
{
case ')':
if (c == '(')
stk.pop();
else
return false;
break;
case ']':
if (c == '[')
stk.pop();
else
return false;
break;
case '}':
if (c == '{')
stk.pop();
else
return false;
break;
}
}
}
if (stk.size() == 0)
return true;
else return false;
}
};
void main()
{
Solution s1;
string s= "]])";//"hello world";
bool result = s1.isValid(s);
// 输出
#include<stack>
#include<string>
using namespace std;
class Solution {
public:
bool isValid(string s) {
int l=s.size();
stack<char> stk;
int j=0,i=0;
char c,d;
for(i=0;i<l;i++)
{
if(s[i]=='('||s[i]=='{'||s[i]=='[')
{
stk.push(s[i]);
}
else
{
c=stk.top();
switch(s[i])
{
case ')':
if (c == '(')
stk.pop();
else
return false;
break;
case ']':
if (c == '[')
stk.pop();
else
return false;
break;
case '}':
if (c == '{')
stk.pop();
else
return false;
break;
}
}
}
if (stk.size() == 0)
return true;
else return false;
}
};
void main()
{
Solution s1;
string s= "]])";//"hello world";
bool result = s1.isValid(s);
// 输出
cout<<result<<endl;
}
此题还是挺简单,不过读栈的TOP时要先判断栈时候为空。