Given a string containing just the characters , determine if the input string is valid.
The brackets must close in the correct order, “()” and “()[]{}” are all valid but “(]” and “([)]” are not.
#include<string>
#include<iostream>#include<unordered_map>
using namespace std;
class solution{
public :
bool true_order(string& strs){
const char* str=strs.c_str();
unordered_map<char, int>hash;
hash['('] = ')';
hash['{'] = '}';
hash['['] = ']';
for (int i = 0; i < strs.size(); i++)
{
char str = strs[i];
if (hash.find(str) != hash.end())//对映射表的键索引,若键不存在返回hash.end(),这是一个指针,iter也是指针,指向迭代器这个对象
{
if (strs[i + 1] != hash[str])//对字符串中的单个字符的索引
return false;
}
}
return true;
}
};
int main()
{
solution s;
string strs = "[]{}(){}(()";
bool re=s.true_order(strs);
cout << re<<endl;
}