-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path20.ValidParentheses.js
More file actions
49 lines (48 loc) · 1.08 KB
/
20.ValidParentheses.js
File metadata and controls
49 lines (48 loc) · 1.08 KB
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
43
44
45
46
47
48
49
/**
* @param {string} s
* @return {boolean}
*/
var isValid = function(s) {
var _s=[];
for(let i=0;i<s.length;i++){
if(s[i]=='(' || s[i]=='[' ||s[i]=='{'){
_s.push(s[i]);
}else{
if(s[i]==')' && _s.pop()!='('){
return false;
}
if(s[i]==']' && _s.pop()!='['){
return false;
}
if(s[i]=='}' && _s.pop()!='{'){
return false;
}
}
}
if(_s.length!=0) return false;
return true;
};
/**
* @param {string} s
* @return {boolean}
*/
var isValid = function(s) {
var _s=[];
for(let i=0;i<s.length;i++){
switch(s[i]){
case ')':
if( _s.pop()!='(') return false;
break;
case '}':
if( _s.pop()!='{') return false;
break;
case ']':
if( _s.pop()!='[') return false;
break;
default:
_s.push(s[i]);
break;
}
}
return _s.length==0;
};