括号匹配度判断

括号匹配度判断

function isMatch(str) {
            let stack = new Stack();
            for (let i = 0; i < str.length; i++) {
                if (str[i] == "(" || str[i] == "[" || str[i] == "{") {
                    stack.push(str[i]);//进栈
                }
                else {
                    switch (str[i]) {
                        case ")":
                            if (stack.pop() != "(") {
                                return false;
                                break;
                            }
                            break;
                        case "}":
                            if (stack.pop() != "{") {
                                return false;
                                break;
                            }
                            break;
                        case "]":
                            if (stack.pop() != "[") {
                                return false;
                                break;
                            }
                            break;

                    }
                }
            }
            if(stack.isEmpty()){
                return true

            }else{
                return false
            }
        }
        console.log(isMatch("[{()}]"));
        console.log(isMatch("[({}))}]"));

 

posted on 2022-08-23 10:12  香香鲲  阅读(14)  评论(0编辑  收藏  举报