Skip to content

Instantly share code, notes, and snippets.

@icameling
Created July 19, 2022 13:26
Show Gist options
  • Save icameling/6c66539b3e2b6835658151edcff9b94b to your computer and use it in GitHub Desktop.
Save icameling/6c66539b3e2b6835658151edcff9b94b to your computer and use it in GitHub Desktop.
#栈与队列 #有效的括号
class Solution {
public:
bool isValid(string s) {
stack<int> check;
map<char, char> bracket_map;
bracket_map['('] = ')';
bracket_map['['] = ']';
bracket_map['{'] = '}';
for (int i = 0; i < s.size(); ++i) {
if (s[i] == '(' || s[i] == '[' || s[i] == '{')
check.push(s[i]);
else {
if (check.empty())
return false;
if (bracket_map[check.top()] == s[i])
check.pop();
else
return false;
}
}
if (check.empty())
return true;
else
return false;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment