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.

解题思路:

由于是O(n)的时间复杂度,因此不能用排序实现,本题有点类似于第一题Java for LeetCode 001 Two Sum也需要用HashMap来实现,具体思路是先把nums装进Map中,然后对nums的每一个元素,检查其“势力范围"(本题不涉及重复元素),JAVA实现如下:

	public int longestConsecutive(int[] nums) {
HashMap<Integer, Boolean> hm = new HashMap<Integer, Boolean>();
int res = 0;
for (int num : nums)
hm.put(num, false);
for (int num : nums) {
if (hm.get(num))
continue;
int width = 1;
for (int j = 1; hm.containsKey(num - j); j++, width++)
hm.put(num - j, true);
for (int j = 1; hm.containsKey(num + j); j++, width++)
hm.put(num + j, true);
res = Math.max(res, width);
}
return res;
}

Java for LeetCode 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. F ...

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

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

  3. leetcode 128. Longest Consecutive Sequence ----- java

    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 (union find)

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

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

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

  7. Leetcode#128 Longest Consecutive Sequence

    原题地址 1. 把所有元素都塞到集合里2. 遍历所有元素,对于每个元素,如果集合里没有,就算了,如果有的话,就向左向右拓展,找到最长的连续范围,同时在每次找的时候都把找到的删掉.这样做保证了同样的连续 ...

  8. 128. Longest Consecutive Sequence(leetcode)

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

  9. 【LeetCode】128. Longest Consecutive Sequence

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

随机推荐

  1. schema设计

    Schema设计   Schema:表的模式:   设计数据的表,索引,以及表和表的关系 在数据建模的基础上将关系模型转为数据库表 满足业务模型需要基础上根据数据库和应用特点优化表结构   关系模型图 ...

  2. NetBeans菜单栏字体太小了

    NetBeans菜单栏字体太小了,导致很难看 解决方法:在netbeans的快捷方式内加入"netbeans.exe" --fontsize 12参数.还可以通过配置NetBean ...

  3. IOS之怎样把自己开发的App安装到越狱的手机

    场景: 有开发人员账号,能够把设备加到开发人员账号中,真机调试.如今须要打包,安装到的越狱手机上(此越狱手机没有加到开发人员账号中,另外公司的人). 常识: 没有越狱的话,最大的问题就是设备的签名,每 ...

  4. python例子

    例一:for循环 for i in range(1,100): if i==23: print "great,%s you got your lucky number:" %(i) ...

  5. 移动端去掉按钮button默认样式

    <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...

  6. 苹果证书的申请、unityoc交互基础

    注冊开发人员账号时:注意不要使用中国邮箱 99美金证书 :仅仅支持上传AppStore. 299美金证书:指的的我开发的应用是仅仅支持打包安装.企业级的. 假设申请了开发人员账号.也就是交了那几百美金 ...

  7. 【Python】字典~深入篇

    字典的定义 字典是一系列键值对,字典用放在{}一系列键值对表示 info = {','city':'KunMing'} 字典增.删.改.查 增加新元素 指定字典名,用方括号括起来的键和相关的值 inf ...

  8. JavaScript-4.1-简单的表单操作,函数用法---ShinePans

    <html> <head> <meta http-equiv="content-type" content="text/html;chars ...

  9. CentOS7.1 KVM虚拟化之经常使用管理虚拟机命令(3)

    一.查看虚拟机列表及状态 [root@kvm01 ~]# virsh list --all Id Name State ---------------------------------------- ...

  10. 连续调用scanf的问题总结

    对于非常简单的scanf函数,一直使用,但是却是有很多的知识点没有掌握好,现总结如下: 1.多个scanf之后,后序以 scanf("%c",&c) 当程序连续调用scan ...