阅读:3280回复:1
javascript 判断括号是否配对
示例::
"()" | "()[]{}" | "{[]}" true pw_{{}([])} "(]" | "([)]" false pw_{{}([[])]} {([)]} */ var str = "pw_{{}([[])]}"; var isValid = function(s){ let items = [] let sLength = s.length if(sLength % 2 !== 0){ return false } for(let i=0; i < sLength; i++){ switch(s){ case "(": items.push(s) break case "[": items.push(s) break case "{": items.push(s) break case ")": if(items[items.length - 1] === "("){ items.pop() //删除 } break case "]": if(items[items.length - 1] === "["){ items.pop() //删除 } break case "}": if(items[items.length - 1] === "{"){ items.pop() //删除 } break } } return items.length === 0 } console.log(isValid(str)) |
|
沙发#
发布于:2022-06-13 19:50
|
|