-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path20.valid-parentheses.cpp
More file actions
42 lines (34 loc) · 893 Bytes
/
20.valid-parentheses.cpp
File metadata and controls
42 lines (34 loc) · 893 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include "testharness.h"
#include <string>
#include <string.h>
#include <stack>
#include <vector>
using namespace std;
class Solution {
public:
bool isValid(string s) {
stack<char> charStack;
for (size_t i = 0; i < s.length(); i++) {
switch (s[i]) {
case '{':
case '[':
case '(':
charStack.push(s[i]);
break;
default:
if (charStack.empty())
return false;
char c = charStack.top();
if ((c == '{' && s[i] == '}') || (c == '[' && s[i] == ']') || (c == '(' && s[i] == ')')) {
charStack.pop();
} else {
return false;
}
}
}
return charStack.empty();
}
};
TEST(Solution, test) {
ASSERT_EQ(2, 1+1);
}