标签:false ring 哈哈 for -- javascrip tac var 开心
此解法用JavaScript写的,然后运行速度在leetcode上秒杀所有人,是迄今为止JavaScript上最快的解法
哈哈哈 很开心~~~
/**
* @param {string} s
* @return {boolean}
*/
var isValid = function(s) {
let stack = [];
var len = 0;
let arr = s.split(‘‘);
for (let i = 0; i < arr.length; i++) {
let str = arr[i];
switch (str) {
case ‘(‘:
stack[len] = str;
len++;
break;
case ‘[‘:
stack[len] = str;
len++;
break;
case ‘{‘:
stack[len] = str;
len++;
break;
case ‘)‘:
if (stack[len-1] == ‘(‘)
len--;
else
return false;
break;
case ‘]‘:
if (stack[len-1] == ‘[‘)
len--;
else
return false;
break;
case ‘}‘:
if (stack[len-1] == ‘{‘)
len--;
else
return false;
break;
}
}
if (len == 0)
return true;
else
return false;
};
标签:false ring 哈哈 for -- javascrip tac var 开心
原文地址:http://www.cnblogs.com/mysteryven/p/7751224.html