Created
July 19, 2022 13:26
-
-
Save icameling/6c66539b3e2b6835658151edcff9b94b to your computer and use it in GitHub Desktop.
#栈与队列 #有效的括号
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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