問題 https://leetcode-cn.com/problems/valid-parentheses/
練習使用JavaScript解答
/**
* @param {string} s
* @return {boolean}
*/
var isValid = function(s) {
if(s == "")
return true;
var arr = [];
var stu = {
'}':'{',
']':'[',
')':'('
};
s = String(s);
for(var i=0;i<s.length;++i) {
switch(s[i]) {
case '(':
case '{':
case '[':
arr.push(s[i]);
break;
case ')':
case '}':
case ']':
var tmp = arr.pop();
if(tmp != stu[s[i]])
return false;
}
}
return arr.length == 0;
};