Last active
April 30, 2019 07:11
-
-
Save bunnyadad/184b1263104ab783ea60a01b1203e7f1 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
#include <string> | |
#include <vector> | |
using namespace std; | |
class Solution { | |
public: | |
bool isValid(string s) | |
{ | |
vector<char> queue; | |
for (auto c : s) | |
{ | |
switch (c) | |
{ | |
case '(': | |
case '[': | |
case '{': | |
queue.emplace_back(c); | |
break; | |
case ')': | |
if (queue.empty() || queue.back() != '(') return false; | |
queue.pop_back(); | |
break; | |
case ']': | |
if (queue.empty() || queue.back() != '[') return false; | |
queue.pop_back(); | |
break; | |
case '}': | |
if (queue.empty() || queue.back() != '{') return false; | |
queue.pop_back(); | |
break; | |
default: | |
return false; | |
} | |
} | |
if (!queue.empty()) return false; | |
return true; | |
} | |
}; | |
int main() | |
{ | |
Solution s; | |
auto o = s.isValid({"([])"}); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Runtime: 4 ms, faster than 100.00% of C++ online submissions for Valid Parentheses.
Memory Usage: 8.5 MB, less than 99.75% of C++ online submissions for Valid Parentheses.