-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDAY0003_2.cpp
More file actions
31 lines (31 loc) · 882 Bytes
/
DAY0003_2.cpp
File metadata and controls
31 lines (31 loc) · 882 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
// 20. Valid Parentheses
class Solution {
public:
vector<char>stack;
void pop(){
if (stack.size()==0) return ;
stack.pop_back();
}
void push(char c){
stack.push_back(c);
}
char peek(){
if(stack.size()==0) return ' ';
return stack[stack.size()-1];
}
bool isValid(string s) {
if (s.size() %2 ==1 )return false;
for(int i=0;i<s.size();i++){
if(s[i]=='('||s[i]=='{'||s[i]=='['){
push(s[i]);
}
else if((s[i]==')'&&peek()=='(')||(s[i]=='}'&&peek()=='{')||(s[i]==']'&&peek()=='[')){
pop();
}
else{
return false;
}
}
return stack.size()==0;
}
};