标签:valid parenthese 符号匹配 leetcode characters
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.
思路:借助vector容器,存放字符’(‘,’{‘,’[‘,从左到右的读取字符串的每一个字符,
如果不是,在确保vector容器有字符的情况下,则让其和vector的最后一个比较;
最后,如果vector元素全部匹配完了,则返回true,否则返回false;
代码如下(c++):
class Solution {
public:
bool isValid(string s) {
vector<char> str;
int len = s.length();
for(int i=0; i<len; i++){
if(isThose(s[i])) {
str.push_back(s[i]);
continue;
}
if(str.size()>0 && isTrue(str[str.size()-1],s[i])) {
cout<<str.size()<<"f"<<isTrue(str[str.size()-1],s[i])<<endl;
str.resize(str.size()-1);
}else{
return false;
}
}
if(str.size() == 0)
return true;
else
return false;
}
bool isThose(char &a){
if(a == ‘{‘ || a == ‘(‘ || a == ‘[‘)return true;
return false;
}
bool isTrue(char &a, char &b){
if( a == ‘(‘ && b == ‘)‘ ) return true;
else if( a == ‘[‘ && b == ‘]‘ ) return true;
else if( a == ‘{‘ && b == ‘}‘ ) return true;
return false;
}
};
Leetcode[20]-Valid Parentheses
标签:valid parenthese 符号匹配 leetcode characters
原文地址:http://blog.csdn.net/dream_angel_z/article/details/46457529