给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效。
有效字符串需满足:
左括号必须用相同类型的右括号闭合。
左括号必须以正确的顺序闭合。
注意空字符串可被认为是有效字符串。
示例 1:
输入: "()"
输出: true
示例 2:
输入: "()[]{}"
输出: true
示例 3:
输入: "(]"
输出: false
示例 4:
输入: "([)]"
输出: false
示例 5:输入: "{[]}"
输出: true
class Solution {
public:
int get_pos(vector<char> v, char r)//此函数用于返回char型在当前向量中的位置
{
for (int i = 0; i < v.size(); i++)
{
if (v[i] == r)
{
return i;
}
}
return 1000;
}
bool isValid(string s) {
vector<char>s1={'(','{','['};
vector<char>s2={')','}',']'};
// stack<char> res;
if (s.size()%2!=0)
return false;
if (count(s2.begin(),s2.end(),s[0])!=0){
return false;
}
stack<char> res;
char top;
int start=0;
while(start<s.size()){
if (count(s1.begin(),s1.end(),s[start])!=0)
{
res.push(s[start]);
start++;
continue;
}
if (count(s2.begin(),s2.end(),s[start])!=0)
{
if (res.empty())
{
return false;
}
top=res.top();
if (get_pos(s1,top)!=get_pos(s2,s[start])){
return false;
}
else{
res.pop();
start++;
}
}
}
if(res.empty()){
return true;
}
else
return false;
}
};
哈希表方法:
class Solution {
public:
bool isValid(string s) {
if(s.length()%2!=0) return false;//一但是奇数说明不是有效的括号
map<char,char> wordbook;//建立哈希表
// wordbook.insert(map<char,char>::value_type(')','('));
// wordbook.insert(map<char,char>::value_type(']','['));
// wordbook.insert(map<char,char>::value_type('}','{'));
wordbook.insert(pair<char,char>(')','('));
wordbook.insert(pair<char,char>(']','['));
wordbook.insert(pair<char,char>('}','{'));
stack<char> mystack;//建立栈
for(int i=0;i<s.length();i++)
{
if(s[i]=='['||s[i]=='{'||s[i]=='(')//匹配到左括号
mystack.push(s[i]);//放入栈中
else if(s[i]==']'||s[i]=='}'||s[i]==')')//匹配到右括号
{
if(mystack.empty()) return false;
//匹配到右括号,栈中应该存在左括号。否则就是无效的括号
if(wordbook[s[i]]==mystack.top())//与栈顶元素进行匹配
{
mystack.pop();//匹配成功删除栈顶元素
continue;
}
else return false;
}
}
if(mystack.empty()) return true;//有效的括号到最后检测结束栈中应没有元素
else return false;
}
};