Created
October 14, 2020 20:49
-
-
Save Robogeek95/6798a1c3475abb125424418725fc17fd to your computer and use it in GitHub Desktop.
20. Valid Parentheses
This file contains hidden or 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<char> parentheses; | |
for (int i = 0; i < s.size(); ++i) { | |
if (s[i] == '(' || s[i] == '[' || s[i] == '{') parentheses.push(s[i]); | |
else { | |
if (parentheses.empty()) return false; | |
if (s[i] == ')' && parentheses.top() != '(') return false; | |
if (s[i] == ']' && parentheses.top() != '[') return false; | |
if (s[i] == '}' && parentheses.top() != '{') return false; | |
parentheses.pop(); | |
} | |
} | |
return parentheses.empty(); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment