题目: Please give a function to check matching pairs of braces, parenthese and bracketsfunction isMatchingPair(str) { // your code here } isMatchingPair('(str[x)xx]') // return false isMatchingPair('({[str]})') // return true
1 回答

守候你守候我
TA贡献1802条经验 获得超10个赞
function isMatchingPair(str) {
var s = [];
var l = ['(','[','{'];
var r = [')',']','}'];
for(var i = 0; i< str.length; i++){
if(l.includes(str[i]))
s.push(r[l.indexOf(str[i])]);
if(r.includes(str[i]))
if(s.pop()!=str[i]){return false;}
}
return s.length?false:true;
}
添加回答
举报
0/150
提交
取消