Valid Parentheses

 1 /**
 2  * @param {string} s
 3  * @return {boolean}
 4  */
 5 var isValid = function(s) {
 6     var stack = [];
 7 
 8     for (var i = 0; i < s.length; i++) {
 9         if (stack.length < 1) {
10             stack.push(s[i]);
11         } else if ((s[i] === ')' && stack[stack.length - 1] === '(') || (s[i] === ']' && stack[stack.length - 1] === '[') || (s[i] === '}' && stack[stack.length - 1] === '{')) {
12             stack.pop();
13         } else {
14             stack.push(s[i]);
15         }
16     }
17 
18     return stack.length < 1;
19 };

 

posted @ 2015-12-24 20:28  叫我霍啊啊啊  阅读(111)  评论(0编辑  收藏  举报