-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path020.js
More file actions
33 lines (31 loc) · 1023 Bytes
/
Copy path020.js
File metadata and controls
33 lines (31 loc) · 1023 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
/**
* @param {string} s
* @return {boolean}
*/
var isValid = function (s) {
if (s === null || s.length === 0) return false;
var strQueue = [], len = s.length;
for (var i = 0; i < len; i++) {
if (s.charAt(i) === '{' || s.charAt(i) === '[' || s.charAt(i) === '(') {
strQueue.push(s.charAt(i));
}
if (s.charAt(i) === '}' || s.charAt(i) === ']' || s.charAt(i) === ')') {
if (strQueue.length === 0) return false;
if (s.charAt(i) === '}' && strQueue[strQueue.length - 1] === '{') {
strQueue.pop();
} else if (s.charAt(i) === ']' && strQueue[strQueue.length - 1] === '[') {
strQueue.pop();
} else if (s.charAt(i) === ')' && strQueue[strQueue.length - 1] === '(') {
strQueue.pop();
} else {
return false;
}
}
}
if(strQueue.length===0){
return true;
}else{
return false;
}
};
console.log(isValid('{[]'))