0

有一个数组[1,1,1,2,3,4,5,8,10,22,24,25,26,66]
请写一个方法把数组变成[1,1,[1,2,3,4,5],8,10,22,[24,25,26],66]
就是里面的顺着的数字归成一个数组,没思路,有没有好的方案?

6个回答

2

已采纳

两个指针 i,j

var arr = [1,1,1,2,3,4,5,8,10,22,24,25,26,66]
var len = arr.length
var i = 1, j = 0
var rst = []
for(; i <= len; i++) {
  if(arr[i]-arr[i-1] !== 1) {
    i-j===1 ? rst.push(arr[j]) : rst.push(arr.slice(j, i))
    j = i
  }
}
1

两个指针不就可以了吗。
注意点就是合并以后后面的指针要减去长度。


function mergeSeq(array) {
    let result = [];
    let arr = [];

    for (let i = 0; i < array.length; i++) {
        let element = array[i];
        arr[0] = element;
        console.log(i);
        for (let j = i + 1; j < array.length; j++) {
            const e = array[j];
            if ((e - element) == 1) {
                arr.push(e);
                element = e;
            } else {
                break;
            }
            i++;
            console.log(i);
        }
        if (arr.length == 1) {
            result.push(arr[0]);
        } else {
            result.push(arr);
        }
        arr = [];
    }
    return result;
}
let array = [
    1,
    1,
    1,
    2,
    3,
    4,
    5,
    8,
    10,
    22,
    24,
    25,
    26,
    66
];
console.log(array.length);
let result = mergeSeq(array);
console.log(result);

快速写了下,没有测试边界

1

折腾了一下,php版本

$arr = [1,1,1,2,3,4,5,8,10,22,24,25,26,66];
$len = count($arr);
sort($arr, SORT_ASC);
$res   = [];
$index = 0;
for ($i = 0; $i < $len - 1; $i++) {
    if ($arr[$i+1] == $arr[$i] + 1) {
        $res[$index][] =  $arr[$i];
        if ($arr[$i+1] + 1 != $arr[$i+2]) {
            $res[$index][] =  $arr[$i+1];
            $i++;
            $index++;
            $res[$index] = $arr[$i+1];
        }
    } else {
        $res[$index] = $arr[$i];
        $index++;
    }
}
echo json_encode($res);
1

贴个js版的

(function(){
let arr = [1,2,3,1,1,2,3,4,5,8,10,22,24,25,26,66,67];
let result = arr.reduce((res, a, i)=>{
    if(i===0){
        res.push(a);
        return res;
    }
    if(a-arr[i-1] === 1){
        if(!Array.isArray(res[res.length-1])){
            res = [...res.slice(0,-1), [...res.slice(-1)]];
        }
        res[res.length-1].push(a);
    }else{
        res.push(a);
    }
    return res;
}, []);
console.log(result);
})();
0
/**
*寻找顺着的数字的规律,或者比前一个数多一,或者比后一个数少一,
*然后分类
*s[i]-1 === s[i-1] || s[i]+1 === s[i+1]
 */
const seq = [1,1,1,2,3,4,5,8,10,22,24,25,26,66]
let rst = []
seq.forEach(function (v, i) {
    if(v+1 === seq[i+1] || v-1 === seq[i-1])
        rst[rst.length -1] instanceof Array ?
        rst[rst.length -1].push(v) :
        rst.push(Array.of(v))
    else
        rst.push(v)
})
console.log(rst)
console.log(seq)
0

php版,菜鸟的想法:

`<?php 
 $arr = [1,1,1,2,3,4,5,8,10,22,24,25,26,66];
 $new_arr = '';
 foreach($arr as $k => $v){
    if($k == 0){
        $new_arr .= $v;
    }else{
        if($arr[$k-1]+1 == $v){
            $new_arr .= ",".$v;
        }else{
            $new_arr .= "#".$v;
        }
    }
 }
 $new_arr_1 = explode("#",$new_arr);
 foreach($new_arr_1 as $k1 => $v1){
    if(strpos($v1,",") !== false){
        $new_arr_2 = explode(",",$v1);
        $new_arr_1[$k1] = $new_arr_2;
    }
 }
 print_r($new_arr_1);
?>`

撰写答案