Skip to content

Commit 6f4aa5f

Browse files
committed
Create 20. Valid Parentheses.cpp
1 parent 2144933 commit 6f4aa5f

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

20. Valid Parentheses.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
unordered_map<char, char> map = {{')','('}, {']', '['}, {'}', '{'}};
4+
bool isValid(string s) {
5+
vector<char> stack;
6+
for(int i = 0; i < s.length(); ++i){
7+
if(s[i] == '(' || s[i] == '{' || s[i] == '[')
8+
stack.push_back(s[i]);
9+
else if(!stack.empty() && map[s[i]] == stack.back())
10+
stack.pop_back();
11+
else return false;
12+
}
13+
return stack.empty();
14+
}
15+
};

0 commit comments

Comments
 (0)