一道前端面试题,求解

题目: Please give a function to check matching pairs of braces, parenthese and brackets

function isMatchingPair(str) {     

    // your code here 

}

 isMatchingPair('(str[x)xx]')  // return false 

 isMatchingPair('({[str]})')  // return true


至尊宝的传说
浏览 470回答 1
1回答

守候你守候我

function isMatchingPair(str) {&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; var s = [];&nbsp; &nbsp; var l = ['(','[','{'];&nbsp; &nbsp; var r = [')',']','}'];&nbsp; &nbsp; for(var i = 0; i< str.length; i++){&nbsp; &nbsp; &nbsp; &nbsp; if(l.includes(str[i]))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s.push(r[l.indexOf(str[i])]);&nbsp; &nbsp; &nbsp; &nbsp; if(r.includes(str[i]))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(s.pop()!=str[i]){return false;}&nbsp; &nbsp; }&nbsp; &nbsp; return s.length?false:true;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript