Skip to content

Instantly share code, notes, and snippets.

@bunnyadad
Last active April 30, 2019 07:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bunnyadad/184b1263104ab783ea60a01b1203e7f1 to your computer and use it in GitHub Desktop.
Save bunnyadad/184b1263104ab783ea60a01b1203e7f1 to your computer and use it in GitHub Desktop.
#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;
}
@bunnyadad
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment