js数组分割代码优化(如下代码),求改进优化或更高效算法
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这样的逻辑启示很有规律,代码中用来辅助的变量太多,不利于阅读和维护,
以下代码不需要记录位置,只需要定义一个二位数组,没有多余的变量,详细请看注释
为什么要写的那么复杂,直接使用临时数据就好了
在遇到0之前把内容放入临时数组中,遇到下一个0后,判断临时数组是否符合要求,
如果符合,放入结果数组并清空临时数组;不符合的直接清空数组,记录下一个非0数值
还有其它需求么?
好像这样就o了,不知题主满意否