js数组分割代码优化(如下代码),求改进优化或更高效算法

发布于 2022-09-01 19:43:40 字数 3845 浏览 10 评论 0

1.数据样本(一个数组):

0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,2,1,1,0,0,0,0,0,6,7,8,8,3,0,0,1,2,5,5,3,0,0,0,0,0,0,2,1,3,3,3,4,3,0,0,1,0,0,0,0,0,0,1,0,1,3,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,2,0,0,2,1,0,0,0,0,0,0,0,3,4,7,11,12,7,5,0,0,0,0,1,1,3,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,3,5,5,5,4,4,3,3,4,7,6,4,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,2,3,2,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,6,8,13,14,16,17,20,21,21,23,27,28,29,31,32,33,33,33,33,33,34,34,34,35,34,34,33,33,33,34,34,35,35,34,34,34,34,33,33,33,33,33,34,34,38,49,59,75,91,107,114,122,127,129,132,139,147,154,172,185,204,210,219,235,238,240,242,244,244,244,246,244,243,242,243,240,236,226,227,210,206,196,185,174,162,154,138,117,117,115,109,95,88,86,76,59,36,31,26,26,26,26,26,26,26,26,26,25,25,25,25,25,25,25,24,22,14,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,10,26,38,53,64,70,89,103,109,114,122,136,152,165,174,184,194,210,222,228,228,229,230,230,232,237,240,240,242,243,242,242,244,245,238,231,219,207,204,197,190,173,172,167,167,166,156,139,127,112,102,91,90,90,90,90,92,92,91,92,90,89,89,88,89,89,89,89,89,90,90,90,90,90,90,91,91,92,92,92,91,90,91,91,93,103,114,126,130,131,147,161,169,168,171,180,192,201,210,222,234,242,244,246,246,247,242,242,241,241,240,240,240,236,230,232,230,223,214,204,185,159,139,123,119,113,109,105,80,69,66,50,37,22,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0

2.目的:得到0作为分割的数组,所以建二维数组,但是数组有要求,要数组长度大于5,比如本来a[0].length =5,a[0]={1,2,2,1,1},但是由于长度小于5舍弃。而且当长度大于5,但是该数组最大值(或平均值)小于一个阈值(比如20),则也省略,比如1,3,5,5,5,4,4,3,3,4,7,6,4,2,这段也无效。
3.以上数据样本要求的处理结果:

arrList[0]={5,6,8,13,14,16,17,20,21,21,23,27,28,29,31,32,33,33,33,33,33,34,34,34,35,34,34,33,33,33,34,34,35,35,34,34,34,34,33,33,33,33,33,34,34,38,49,59,75,91,107,114,122,127,129,132,139,147,154,172,185,204,210,219,235,238,240,242,244,244,244,246,244,243,242,243,240,236,226,227,210,206,196,185,174,162,154,138,117,117,115,109,95,88,86,76,59,36,31,26,26,26,26,26,26,26,26,26,25,25,25,25,25,25,25,24,22,14},
arrList[1]={10,26,38,53,64,70,89,103,109,114,122,136,152,165,174,184,194,210,222,228,228,229,230,230,232,237,240,240,242,243,242,242,244,245,238,231,219,207,204,197,190,173,172,167,167,166,156,139,127,112,102,91,90,90,90,90,92,92,91,92,90,89,89,88,89,89,89,89,89,90,90,90,90,90,90,91,91,92,92,92,91,90,91,91,93,103,114,126,130,131,147,161,169,168,171,180,192,201,210,222,234,242,244,246,246,247,242,242,241,241,240,240,240,236,230,232,230,223,214,204,185,159,139,123,119,113,109,105,80,69,66,50,37,22,4}

4.原先代码(不包括对数组长度要求和长度要求后的数组最大要求)如下(需改进):

var arrList = new Array();
        var k = -1;
        var j = 0;
        //记录每个数字起始位置的下标值
        var startPosition = new Array();
        
        for (var i = 0; i < tempColumns.length - 1; i++){
            //0到其他数字的过度
            //判断数字串分割后,如果长度小于一个值(k,k要小于矩阵小块划分值),则为噪音
            if ((tempColumns[i] == 0) && (tempColumns[i + 1] != 0)) { //0,13,30...
                k++;
                arrList[k] = new Array(); //分割函数段 
                j = 0;
                startPosition[k] = i + 1; //跟分段长度一样,起始点跟长度分割点有关
            } else if (tempColumns[i] != 0) {  
                //alert("tempColumns:"+tempColumns[i]);
                arrList[k][j] = tempColumns[i]; //这段经常报错!!!!!
                j++;
            } else {}
        }

startPosition 记录有效数组段在老数组中的起始位置。
希望有高手可以给些建议,或者有更好的算法实现思路提供,谢谢!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

桃酥萝莉 2022-09-08 19:43:40

这样的逻辑启示很有规律,代码中用来辅助的变量太多,不利于阅读和维护,
以下代码不需要记录位置,只需要定义一个二位数组,没有多余的变量,详细请看注释

var arr = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,2,1,1,0,0,0,0,0,6,7,8,8,3,0,0,1,2,5,5,3,0,0,0,0,0,0,2,1,3,3,3,4,3,0,0,1,0,0,0,0,0,0,1,0,1,3,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,2,0,0,2,1,0,0,0,0,0,0,0,3,4,7,11,12,7,5,0,0,0,0,1,1,3,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,3,5,5,5,4,4,3,3,4,7,6,4,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,2,3,2,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,6,8,13,14,16,17,20,21,21,23,27,28,29,31,32,33,33,33,33,33,34,34,34,35,34,34,33,33,33,34,34,35,35,34,34,34,34,33,33,33,33,33,34,34,38,49,59,75,91,107,114,122,127,129,132,139,147,154,172,185,204,210,219,235,238,240,242,244,244,244,246,244,243,242,243,240,236,226,227,210,206,196,185,174,162,154,138,117,117,115,109,95,88,86,76,59,36,31,26,26,26,26,26,26,26,26,26,25,25,25,25,25,25,25,24,22,14,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,10,26,38,53,64,70,89,103,109,114,122,136,152,165,174,184,194,210,222,228,228,229,230,230,232,237,240,240,242,243,242,242,244,245,238,231,219,207,204,197,190,173,172,167,167,166,156,139,127,112,102,91,90,90,90,90,92,92,91,92,90,89,89,88,89,89,89,89,89,90,90,90,90,90,90,91,91,92,92,92,91,90,91,91,93,103,114,126,130,131,147,161,169,168,171,180,192,201,210,222,234,242,244,246,246,247,242,242,241,241,240,240,240,236,230,232,230,223,214,204,185,159,139,123,119,113,109,105,80,69,66,50,37,22,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
var arrS=[];//定义一个数组
for(var i=0;i<arr.length-1;i++){
    if(arr[i]!=0){ //不为0的数据
        if(i==0||arr[i-1]==0){ //自己不为0,上一项为0,说明是需要的数据段的头部
            arrS.push([]); //给arrS创建一个新的类型也是数组的项
        }
        arrS[arrS.length-1].push(arr[i]);//将数据保存到当前二维数组的当前数组项
    }
    if(arr[i]==0&&arr[i-1]!=0&&i!=0){ //到达需要的数据段的尾部
        //检查当前数组项是否满足题主的两个条件,不满足则丢弃当前数组项
        if(arrS[arrS.length-1].length<=5||Math.max.apply(Math, arrS[arrS.length-1])<=20){
            arrS.pop();
        }
    }

}

console.log(arrS);

你的他你的她 2022-09-08 19:43:40

为什么要写的那么复杂,直接使用临时数据就好了
在遇到0之前把内容放入临时数组中,遇到下一个0后,判断临时数组是否符合要求,
如果符合,放入结果数组并清空临时数组;不符合的直接清空数组,记录下一个非0数值
还有其它需求么?

/**
 * Created by akong on 15/10/28.
 * 目的:得到0作为分割的数组,所以建二维数组,但是数组有要求,要数组长度大于5,比如本来a[0].length =5,a[0]={1,2,2,1,1},
 * 但是由于长度小于5舍弃。而且当长度大于5,但是该数组最大值(或平均值)小于一个阈值(比如20),则也省略,比如1,3,5,5,5,4,4,3,3,4,7,6,4,2,这段也无效。
 *
 *
 *
 */

var orginalArray=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,2,1,1,0,0,0,0,0,6,7,8,8,3,0,0,1,2,5,5,3,0,0,0,0,0,0,2,1,3,3,3,4,3,0,0,1,0,0,0,0,0,0,1,0,1,3,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,2,0,0,2,1,0,0,0,0,0,0,0,3,4,7,11,12,7,5,0,0,0,0,1,1,3,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,3,5,5,5,4,4,3,3,4,7,6,4,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,2,3,2,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,6,8,13,14,16,17,20,21,21,23,27,28,29,31,32,33,33,33,33,33,34,34,34,35,34,34,33,33,33,34,34,35,35,34,34,34,34,33,33,33,33,33,34,34,38,49,59,75,91,107,114,122,127,129,132,139,147,154,172,185,204,210,219,235,238,240,242,244,244,244,246,244,243,242,243,240,236,226,227,210,206,196,185,174,162,154,138,117,117,115,109,95,88,86,76,59,36,31,26,26,26,26,26,26,26,26,26,25,25,25,25,25,25,25,24,22,14,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,10,26,38,53,64,70,89,103,109,114,122,136,152,165,174,184,194,210,222,228,228,229,230,230,232,237,240,240,242,243,242,242,244,245,238,231,219,207,204,197,190,173,172,167,167,166,156,139,127,112,102,91,90,90,90,90,92,92,91,92,90,89,89,88,89,89,89,89,89,90,90,90,90,90,90,91,91,92,92,92,91,90,91,91,93,103,114,126,130,131,147,161,169,168,171,180,192,201,210,222,234,242,244,246,246,247,242,242,241,241,240,240,240,236,230,232,230,223,214,204,185,159,139,123,119,113,109,105,80,69,66,50,37,22,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];


//结果数组最小长度
var resultArrRequireMinLength=5;
var maxValueHolder=20;
var averValueHoder=20;

var orginalArrayLength=orginalArray.length;
var i= 0,tempValue;
var resultArray=[],tempArray=[];


var validatorFun=function(fun){

    return function(value){
        return fun.apply(null,value);
    }
};

var averageFun=function(){
    var args=[].slice.apply(arguments);
    var totalValue=0;
    for(var i=0;i<args.length;i++){
        totalValue=totalValue+args[i]
    }
    return Math.round(totalValue/args.length);

};

for(i=0;i<orginalArrayLength;i++){
    tempValue=orginalArray[i];
    if(tempValue===0){
        if(tempArray.length>resultArrRequireMinLength){
            if(validatorFun(Math.max)(tempArray)>=maxValueHolder
                ||validatorFun(averageFun)(tempArray)>=averValueHoder
                ){
                resultArray.push(tempArray);
            }
        }
        tempArray=[];

    }else{
        tempArray.push(tempValue);
    }
}


console.log(resultArray);
花之痕靓丽 2022-09-08 19:43:40
var list = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,2,1,1,0,0,0,0,0,6,7,8,8,3,0,0,1,2,5,5,3,0,0,0,0,0,0,2,1,3,3,3,4,3,0,0,1,0,0,0,0,0,0,1,0,1,3,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,2,0,0,2,1,0,0,0,0,0,0,0,3,4,7,11,12,7,5,0,0,0,0,1,1,3,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,3,5,5,5,4,4,3,3,4,7,6,4,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,2,3,2,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,6,8,13,14,16,17,20,21,21,23,27,28,29,31,32,33,33,33,33,33,34,34,34,35,34,34,33,33,33,34,34,35,35,34,34,34,34,33,33,33,33,33,34,34,38,49,59,75,91,107,114,122,127,129,132,139,147,154,172,185,204,210,219,235,238,240,242,244,244,244,246,244,243,242,243,240,236,226,227,210,206,196,185,174,162,154,138,117,117,115,109,95,88,86,76,59,36,31,26,26,26,26,26,26,26,26,26,25,25,25,25,25,25,25,24,22,14,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,10,26,38,53,64,70,89,103,109,114,122,136,152,165,174,184,194,210,222,228,228,229,230,230,232,237,240,240,242,243,242,242,244,245,238,231,219,207,204,197,190,173,172,167,167,166,156,139,127,112,102,91,90,90,90,90,92,92,91,92,90,89,89,88,89,89,89,89,89,90,90,90,90,90,90,91,91,92,92,92,91,90,91,91,93,103,114,126,130,131,147,161,169,168,171,180,192,201,210,222,234,242,244,246,246,247,242,242,241,241,240,240,240,236,230,232,230,223,214,204,185,159,139,123,119,113,109,105,80,69,66,50,37,22,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    i = -1,
    len = list.length,
    mark1 = 0,
    mark2 = 0,
    temp = [],
    res = [];

while (++i < len) {
    if (list[i] === 0) {
        if (mark1 > 5 && mark2 / mark1 > 20) {
            res.push(temp);
        }
        
        mark1 = 0;
        mark2 = 0;
        temp = [];
    } else {
        mark1 += 1;
        mark2 += list[i];

        temp.push(list[i]);
    }
}

console.log(res);

好像这样就o了,不知题主满意否

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文