博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Longest Consecutive Sequence
阅读量:4074 次
发布时间:2019-05-25

本文共 705 字,大约阅读时间需要 2 分钟。

Longest Consecutive Sequence

Given an unsorted array of integers, find the length of the longest consecutive elements sequence.

For example,
Given [100, 4, 200, 1, 3, 2],
The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4.

Your algorithm should run in O(n) complexity.

Java代码:

public class Solution {    public int longestConsecutive(int[] num) {        Arrays.sort(num);		int len = num.length;		int max = 1;		int count = 1;		for (int i = 0; i < len - 1; i++) {			if (num[i] + 1 != num[i + 1]) {				if (num[i] == num[i + 1])					continue;				if (max < count)					max = count;				count = 1;			} else {				count++;			}		}		if (max < count) {			max = count;		}		return max;    }}

转载地址:http://siuni.baihongyu.com/

你可能感兴趣的文章
loader如果你提前设width或height,loadComplete后显示不出来
查看>>
如果Stage不是NoScale模式,那么接收不到Event.Resize事件
查看>>
cygwin高速下载网址
查看>>
Flash调用Alchemy编译的代码时出现Error #1506的解决
查看>>
史上最正确的achemy安装方法
查看>>
As3中实现卡马克卷轴算法
查看>>
Flash中实现语音变声(下)
查看>>
StageVideo API
查看>>
[转]三维成像原理
查看>>
Flex Custom Component LifeCycle
查看>>
获取.fla所有导出类名称列表的方法
查看>>
关于FLASH 3D游戏的想法,做一个双人合作射击的游戏,
查看>>
PNG图片优化技术(一)
查看>>
photoshop 优化 PNG 图片尺寸大小 终极秘技!
查看>>
mmo游戏开发应在profile下运行,才能保证正式运行不卡
查看>>
关于Flash CS3创建Sprite类型的问题
查看>>
AS3通俗教程---AS3自身loading制作
查看>>
0 bytes after compression出现的情况
查看>>
内存回收专题
查看>>
[资料] 史上最强的伯克利大学1024线飞龙AI下载地址,有没有人有兴趣来测试一手?...
查看>>