标签:leetcode
链接:https://leetcode.com/problems/valid-parentheses/
问题描述:
Given a string containing just the characters ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[’ and ‘]’, determine if the input string is valid.
The brackets must close in the correct order, “()” and “()[]{}” are all valid but “(]” and “([)]” are not.
Hide Tags Stack String
Hide Similar Problems (M) Generate Parentheses (H) Longest Valid Parentheses
求小括号,中括号,大括号是否对称使用。
class Solution {
public:
bool isValid(string s) {
stack<char> m;
char c;
for(int i=0;i<s.length();i++)
{
if(s[i]==‘(‘||s[i]==‘[‘||s[i]==‘{‘)
m.push(s[i]);
else
{
if(m.empty())
return false;
if((m.top()==‘(‘&&s[i]==‘)‘)||(m.top()==‘[‘&&s[i]==‘]‘)||(m.top()==‘{‘&&s[i]==‘}‘))
m.pop();
else
return false;
}
}
return m.empty();
}
};
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:leetcode
原文地址:http://blog.csdn.net/efergrehbtrj/article/details/46838335