怎么样过滤出一组号码中的顺子号码?

比如有 1,2,5,6,7,11,14这组号码,怎么过滤出[1,2]和[5,6,7]这两组号码?
求指点

月关宝盒
浏览 321回答 5
5回答

交互式爱情

这种逻辑?var arr = [1, 2, 5, 6, 7, 11, 14];var len = arr.length,&nbsp; &nbsp; before = arr[0],&nbsp; &nbsp; i = 1,&nbsp; &nbsp; res = [],&nbsp; &nbsp; result = [],&nbsp; &nbsp; current;for (;i < len; i++) {&nbsp; &nbsp; current = arr[i];&nbsp; &nbsp; if (current - before === 1 ) {&nbsp; &nbsp; &nbsp; &nbsp; if (res.length === 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; res.push(before);&nbsp; &nbsp; &nbsp; &nbsp; res.push(current);&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; if (res.length) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result.push(res);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; res = [];&nbsp; &nbsp; }&nbsp; &nbsp; before = current;}if (res.length) {&nbsp; &nbsp; result.push(res);}console.log(result);

慕森卡

这样行吗?function subsequence(arr) {&nbsp; &nbsp; if (!arr || arr.length < 2) return [];&nbsp; &nbsp; let result = [],&nbsp; &nbsp; &nbsp; &nbsp; curr = arr[0],&nbsp; &nbsp; &nbsp; &nbsp; subArr = [curr];&nbsp; &nbsp; for (let i = 1; i < arr.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; if (arr[i] === curr + 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; subArr.push(arr[i]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; curr = arr[i];&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (subArr.length > 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result.push(subArr);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; curr = arr[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; subArr = [curr];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; if (subArr.length > 1) {&nbsp; &nbsp; &nbsp; &nbsp; result.push(subArr);&nbsp; &nbsp; }&nbsp; &nbsp; return result;}// testconsole.log(subsequence([]));console.log(subsequence([0]));console.log(subsequence([1,2,5,6,7,11,14]));console.log(subsequence([1,2,3,4,7,8,9,11,23,45,56,66,67]));console.log(subsequence([9, 8, 7, 7, 8, 9, 0]));

Cats萌萌

C++来凑个热闹。#include <algorithm>#include <iostream>#include <iterator>#include <vector>template <class It>std::vector<std::vector<int>> Solution(It first, It last) {&nbsp; std::vector<std::vector<int>> result;&nbsp; while (first != last) {&nbsp; &nbsp; auto ite = std::adjacent_find(first, last, [](int l, int r) {return l != r-1;});&nbsp; &nbsp; if (ite != last)&nbsp; &nbsp; &nbsp; ++ite;&nbsp; &nbsp; if (std::distance(first, ite) > 1)&nbsp; &nbsp; &nbsp; result.emplace_back(first, ite);&nbsp; &nbsp; first = ite;&nbsp; }&nbsp; return result;}int main() {&nbsp; std::vector<int> vtr = {1, 2, 3, 5, 1, 2, 1};&nbsp; for (const auto &result : Solution(vtr.begin(), vtr.end())) {&nbsp; &nbsp; std::copy(result.begin(), result.end(), std::ostream_iterator<int>(std::cout, " "));&nbsp; &nbsp; std::cout << std::endl;&nbsp; }&nbsp; return 0;}

繁星点点滴滴

&nbsp; &nbsp; const arr = [1, 2, 3, 4, 7, 8, 9, 11, 23, 45, 56, 66, 67],&nbsp; &nbsp; &nbsp; &nbsp; len = arr.length;&nbsp; &nbsp; let i = 0,&nbsp; &nbsp; &nbsp; &nbsp; c = -1;&nbsp; &nbsp; let newArr = [];&nbsp; &nbsp; while (i < len) {&nbsp; &nbsp; &nbsp; &nbsp; let t = arr[i] - i;&nbsp; &nbsp; &nbsp; &nbsp; if (t === c) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newArr.pop();&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newArr.push(arr[i]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c = t;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; i = i + 1;&nbsp; &nbsp; }&nbsp; &nbsp; console.log(newArr);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript