随心情更新的学习笔记——JS代码之栈解决括号不匹配

这是有上期,没下期,学习看心情,更新看随机的老王的学习笔记,好好看,没有下期了……

 

括号不匹配问题,就比如说一个数学方程式中,一般会有多个括号,而且有的是后括号的类型还会不相同,那么怎么利用栈来检查这个公式中的括号是否匹配,是否缺失呢?

简单思路:

将一段字符串进行遍历,点遍历到“(”、“[”、“{”的时候,将这些字符push到栈中,在字符串中判断是否含有“)”、“]”、“}”这些符号,如果有,就到栈中进行判断,是否符合,顺序是否出错。

 

代码:

function check(array,stack) {
        var length = array.length;
        let buffer;
        for (var i=0;i<length;i++){
            if(array[i]=='('||array[i]=='['||array[i]=='{')
                stack.push(array[i]);
            if (array[i]==')'||array[i]==']'||array[i]=='}'){
                switch(array[i]){
                    case ')':
                        buffer = stack.peek();
                        if(buffer=='('){
                            stack.pop();
                            console.log("()匹配成功");
                        }else{
                            console.log("()匹配错误");
                        }
                        break;
                    case ']':
                        buffer = stack.peek();
                        if(buffer=='['){
                            stack.pop();
                            console.log("[]匹配成功");
                        }else{
                            console.log("[]匹配错误");
                        }
                        break;
                    case '}':
                        buffer = stack.peek();
                        if(buffer=='{'){
                            stack.pop();
                            console.log("{}匹配成功");
                        }else{
                            console.log("{}匹配错误");
                        }
                        break;
                }
            }
        }
    }


    var stack = new Stack();
    check("1+{2+[(12+3)+4]}+9",stack);

    stack.forEach(function (item) {
        console.log(item);
    });

 

#########################################附上老师的代码########################################

function matchBracket(data) {
        var index = -1;
        //把字符串转为数组
        var buffer = data.split('');
        //获得数组的长度
        var length = buffer.length;
        for(var i=0;i<length;i++){
            //获得其中的每个字符
            var word = buffer[i];
            if(['{','[','('].indexOf(word)!=-1){
                stack.push(word);
            }else{
                //{   }   [   ]   (    )
                //2+4+{2*3}
                var peek = stack.peek();
                switch (word){
                    case '}':
                        if(peek=='{'){
                            stack.pop();
                        }else{
                            index=i;
                            return i;
                        }
                        break;
                    case ']':
                        if(peek=='['){
                            stack.pop();
                        }else{
                            index=i;
                            return i;
                        }
                        break;
                    case ')':
                        if(peek=='('){
                            stack.pop();
                        }else{
                            index=i;
                            return i;
                        }
                        break;
                }
            }
        }
    }

 

其中栈的代码用的为:

function Stack() {
    this.dataStore=[];
    this.push=function (data) {
        //获得当前的下标
        var index = this.dataStore.length;
        //添加数据
        this.dataStore[index]=data;
        //console.log(this.dataStore[index]);
        //this.dataStore.push(data);
    };

    this.pop=function () {
        return this.dataStore.pop();
    };

    this.peek=function () {
        //返回数据
        return  this.dataStore[this.dataStore.length-1];
    }

    this.clear=function () {
        delete  this.dataStore;
        this.dataStore=[];
    };

    this.length=function () {
        return this.dataStore.length;
    }

    this.empty=function () {
        var length = this.dataStore.length;
        if(length>0){
            return false;
        }else{
            return true;
        }
    }

    this.forEach=function (call) {
        //1,获得数组的长度
        var length = this.dataStore.length;
        //2,遍历数据
        for(var i=length-1;i>=0;i--){
            // var item= this.dataStore[i];
            call(this.dataStore[i]);
        }
    }

    this.change=function () {
        var length = this.length();
        var result = this.dataStore[length-1];
        var index=0;
        var buffer = [];
        while (result>0){
            result = Math.floor(result/2);
            index++;
        }
        result = this.dataStore[length-1];
        for(var i=0;i<index;i++){
            buffer[i] = Math.floor(result%2);
            result = Math.floor(result/2);
        }
        //return buffer.pop();
        var length2 = buffer.length;
        for (var j=0;j<length2;j++){
            //console.log(j);
            return buffer.pop();
        }
    }
}

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值