给定一个未排序的整数数组,找出最长连续序列的长度。
例如,
给出 [100, 4, 200, 1, 3, 2],
这个最长的连续序列是 [1, 2, 3, 4]。返回所求长度: 4。
要求你的算法复杂度为 O(n)。
详见:https://leetcode.com/problems/longest-consecutive-sequence/description/

Java实现:

方法一:

class Solution {
public int longestConsecutive(int[] nums) {
int res=0;
Set<Integer> s=new HashSet<Integer>();
for(int num:nums){
s.add(num);
}
for(int num:nums){
if(s.remove(num)){
int pre=num-1;
int next=num+1;
while(s.remove(pre)){
--pre;
}
while(s.remove(next)){
++next;
}
res=Math.max(res,next-pre-1);
}
}
return res;
}
}

方法二:

class Solution {
public int longestConsecutive(int[] nums) {
int res = 0;
Map<Integer, Integer> m = new HashMap<Integer, Integer>();
for (int num : nums) {
if (!m.containsKey(num)){
int pre = m.containsKey(num - 1) ? m.get(num - 1) : 0;
int next = m.containsKey(num + 1) ? m.get(num + 1) : 0;
int sum = pre + next + 1;
m.put(num, sum);
res = Math.max(res, sum);
m.put(num - pre, sum);
m.put(num + next, sum);
}
}
return res;
}
}

参考:https://www.cnblogs.com/grandyang/p/4276225.html

128 Longest Consecutive Sequence 一个无序整数数组中找到最长连续序列的更多相关文章

  1. LeetCode 128 Longest Consecutive Sequence 一个无序整数数组中找到最长连续序列

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

  2. Java算法-------无序数组中的最长连续序列---------leetcode128

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

  3. [LeetCode] 128. Longest Consecutive Sequence 解题思路

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

  4. [leetcode]128. Longest Consecutive Sequence最长连续序列

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

  5. [LeetCode] 128. Longest Consecutive Sequence 求最长连续序列

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

  6. 128. Longest Consecutive Sequence(leetcode)

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

  7. 【LeetCode】128. Longest Consecutive Sequence

    Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...

  8. 128. Longest Consecutive Sequence *HARD* -- 寻找无序数组中最长连续序列的长度

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

  9. 128. Longest Consecutive Sequence

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

随机推荐

  1. jquery一个比较好的轮播图jQuery.kinMaxShow介绍

    kinMaxShow API 可选参数以及详解 kinMaxShow 主参数详解 参数名称 默认值 简单释义 height 500 [整型 (单位:像素)]焦点图高度,必须设置 缺省则启用默认高度 5 ...

  2. XML复习笔记(复习资料为菜鸟教程里的XML教程)

    XML 指可扩展标记语言(eXtensible Markup Language) XML 的设计宗旨是传输数据,而不是显示数据. XML 标签没有被预定义.您需要自行定义标签. XML 被设计为具有自 ...

  3. HDU 2444 The Accomodation of Students(判断二分图+最大匹配)

    The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  4. linux网络配置及IP绑定

    在学习时,参考了这篇文章:http://blog.csdn.net/collection4u/article/details/14127671:在这篇文章中作者讲述了VMware中虚机的三种网络模式: ...

  5. HDU 1081:To The Max

    To The Max Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total ...

  6. YTU 2419: C语言习题 等长字符串排序

    2419: C语言习题 等长字符串排序 时间限制: 1 Sec  内存限制: 128 MB 提交: 650  解决: 249 题目描述 在主函数中输入n(n<=10)个等长的字符串.用另一函数对 ...

  7. 织梦CMS被挂马特征汇总

    一.织梦CMS被挂马特征汇总 2013织梦CMS被挂马特征汇总.最近很多朋友反应后台多了几个系统管理员用户:service.spider等,而且自己之前的管理员用户登陆时候会提示用户名不存在.还有朋友 ...

  8. setTimeout的第三个参数

    最熟悉的地方,往往会忽略一些细节.就比如 setTimeout 函数,做前端开发的同学都会很熟悉这个函数,经常使用这个函数,但是知道这个函数还有第三个参数的小伙伴可能就不多了.起码我在阅读阮老师的 e ...

  9. POJ3304:Segments (几何:求一条直线与已知线段都有交点)

    Given n segments in the two dimensional space, write a program, which determines if there exists a l ...

  10. NOIP2007普及 守望者的逃离

    传送门 普及组的题目……很水. 原来写了一个模拟不过好像状态考虑的不全得了80,这次我们考虑一下dp做法. 守卫者有两种移动的方法,一种是闪现,一种是跑,我们可以把闪现和跑分开处理. 首先只处理闪现的 ...