如何找到数组中最长的连续数字链 [英] How to find the longest consecutive chain of numbers in an array

查看:71
本文介绍了如何找到数组中最长的连续数字链的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我们有 [0,1,3,5,7,8,8,9,10,12,13] .

结果必须为 7、8、9、10 ,因为它们彼此相邻,按索引排列并且是连续的整数,并且此链长于 0、1 .

The result must be 7, 8, 9, 10 because they are adjacent to each other, index wise and are consecutive integers, and also this chain is longer than 0, 1.

英语不是我的母语,如果写作有点晦涩,请原谅.

English is not my first language, excuse me if the writing is a bit obscure.

推荐答案

使用 itertools.groupby 基于不断增加的计数(由您可以在结果中包含组键(被抑制为 _ ),以进一步了解其工作原理.

You may include the group key in the result (suppressed as _) to further understand how this works.

这篇关于如何找到数组中最长的连续数字链的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆