您的位置:首页 > 其它

leetcode 20 简单括号匹配

2016-09-28 14:55 453 查看
栈的运用

class Solution {
public:
bool isValid(string s) {
stack<char>The_Stack;
int i=0;
The_Stack.push('#');
while(i<s.size()) {
if((s[i]==')'&&The_Stack.top()=='(')||(s[i]==']'&&The_Stack.top()=='[')||(s[i]=='}'&&The_Stack.top()=='{')) {
i++;
The_Stack.pop();
}
else  {
The_Stack.push(s[i]);
i++;
}
}
if(The_Stack.top()=='#')
return true;
return false;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: