# 请教一道算法题，如下，谢谢！

all2005 2017年11月14日提问
2

## 15个回答

14

``````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
}
}``````
4

``````from itertools import groupby

arr = [1,1,1,2,3,4,5,8,10,22,24,25,26,66]

print([j[0] if len(j) == 1 else j for j in [[i[1] for i in g] for k, g in groupby(enumerate(arr), lambda x: x[1] - x[0])]])``````

``````arr = [1,1,1,2,3,4,5,8,10,22,24,25,26,66]
tmp = [arr[0]]
res = []
length = len(arr)
for i in range(1, length):
if arr[i] - arr[i-1] == 1:
tmp.append(arr[i])
else:
res.append(tmp[0] if len(tmp) == 1 else tmp)
tmp = [arr[i]]
res.append(tmp[0] if len(tmp) == 1 else tmp)``````
3

``````(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);
})();``````
1

Xeira · 2017年11月16日

2

``````func resort(arr []int) interface{} {
if len(arr) == 1 {
return arr
}
i, j := 1, 0
subArr := []interface{}{}
for ; i < len(arr); i++ {
if arr[i] != arr[i - 1] + 1 {
if j == i - 1 {
subArr = append(subArr, arr[i - 1])
} else {
subArr = append(subArr, arr[j:i])
}
j = i
}
}
return subArr
}``````
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

``````\$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);``````
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);
?>```````
0
``````var newArr = arr.reduce((array, item, index) => {
if(arr[index+1] - item === 1 || item - arr[index-1] === 1) {
Array.isArray(array[array.length-1]) ? array[array.length-1].push(item) : array.push([item])
}else {
array.push(item)
}
return array
}, [])``````
0

# PYTHON

``````lala = [1,1,1,2,3,4,5,8,10,22,24,25,26,66]
res = []
tmp = [lala[0]]
for i in range(1, lala.__len__()):
if lala[i] - lala[i-1] == 1:
tmp.append(lala[i])
else:
if len(tmp) == 1:
res.append(tmp[0])
tmp = [lala[i]]
else:
res.append(tmp)
tmp = [lala[i]]

print(res)
``````
0

``````vector<vector<int>> groupNum(vector<int>& nums) {
sort(nums.begin(), nums.end()); // in case nums is not in order
vector<vector<int>> res;
for(int i : nums) {
if (res.empty() || res.back().back() != i - 1)
res.push_back(vector<int>({i}));
else
res.back().push_back(i);
}
return res;
}``````
0

php版本:

``````<?php

\$arr = [1,1,1,2,3,4,5,8,10,22,24,25,26,66];

function sorted_arr(\$arr) {
\$new_arr = [];
\$sorted_arr= [];
foreach (\$arr as \$index => \$number) {
if ((isset(\$arr[\$index + 1]) && \$number + 1 == \$arr[\$index + 1]) || (isset(\$arr[\$index - 1]) && \$arr[\$index - 1] + 1 == \$number)) {
array_push(\$sorted_arr, \$number);
} else if (!empty(\$sorted_arr)){
array_push(\$new_arr, \$sorted_arr);
\$sorted_arr = [];
} else {
array_push(\$new_arr, \$number);
}
}
return \$new_arr;
}

?>
``````
0

python

``````a = [1, 1, 1, 2, 3, 4, 5, 8, 10, 22, 24, 25, 26, 66]
b = []
i = 1
while (i < len(a)):
if a[i] - a[i - 1] == 1:
b.append([a[i - 1]])
for j in range(i, len(a)):
if a[j] - a[j - 1] == 1:
b[-1].append(a[j])
else:
i = j
break
else:
b.append(a[i])
i += 1

print(a)
print(b)
``````

``````a = [1, 1, 1, 2, 3, 4, 5, 8, 10, 22, 24, 25, 26, 66, 67, 67]
b = []
i = 1
while (i < len(a)):
if a[i] - a[i - 1] == 1:
b.append([])
for j in range(i, len(a)):
i += 1
if a[j] - a[j - 1] == 1:
b[-1].append(a[j - 1])
print(i)
if j == len(a) - 1:
b[-1].append(a[j])
break
else:
b[-1].append(a[j - 1])
if j == len(a) - 1:
b.append(a[j])
break
else:
b.append(a[i - 1])
i += 1

print(a)
print(b)``````

0
``````\$arr = [1,1,2,3,4,5,8,10,22,24,25,26,66];
\$number = count(\$arr);
\$last_arr = [];
\$temp_arr = [];
for (\$i=1;\$i<=\$number;\$i++){
if(\$arr[\$i]-\$arr[\$i-1]==1){
\$temp_arr[]=\$arr[\$i-1];
}else{
if(\$temp_arr){
\$temp_arr[] = \$arr[\$i-1];
\$last_arr[] = \$temp_arr;
\$temp_arr = [];
}else{
\$last_arr[] = \$arr[\$i-1];
}
}
}
dump(\$last_arr);``````
0

``````def func(a):
"""
a 输入的数组
"""
temp = [a[0]] #存放临时数组
res = [] # 为了不修改原来的数组a 建一个新的list
i = 1 # i指向前下一个一个数字
while i < len(a):
j = i - 1 # 指向前一个数字
if a[i] - a[j] == 1:
temp.append(a[i]) # 与前一个数字连续
else: # 如果不连续
if len(temp) == 1: #只有一个元素
res.extend(temp)
else: # 多个元素的时候
res.append(temp)
temp = [a[i]] # 重新开始
i += 1
if len(temp):
if len(temp) == 1: #只有一个元素
res.extend(temp)
else: # 多个元素的时候
res.append(temp)
return res

if __name__ == "__main__":
a = [1,1,1,2,3,4,5,8,10,22,24,25,26,66]
res = func(a)
print(res)``````