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.

有难度的一题,想不到HashMap的话很难去实现.

想象一个无穷长度的数组,下标可以为任意值,记录的是数组下标所在连续数字序列的长度,初始均为0。那么,当新进入一个数字X时,需要做以下事情:

1.把X对应的值置为1.

2.查看X-1的值,得到与X连着的左半部分长度,记为low

3. 查看X+1的值,得到与X连着的右半部分长度,记为high。

4. low+high+1 即为X所在连续数字序列的长度。如果比目前最大值大则更新。

5. 更新整个连续数字序列。

针对步骤5,还可以继续优化。对于这个问题,处于连续数字序列内部的值是没有意义的,因为新进入一个数字后我们只会考察与它相邻的位的情况,换句话说对于一个连续数字序列,只有处于两头的值才是有意义的,所以,每次更新数字序列长度时只要更新这两个值即可。

这两个值得下标为X-low 和X+high。

HashMap本质上也是个数组。与以上思路区别在于,无法初始为0,所以,在查看X-1和X+1之前,需要判断它们是否在HashMap中,不在则视为值为0.

最后,如果遇到相同数字则直接忽略。

代码:

     public int longestConsecutive(int[] num) {
HashMap<Integer, Integer> hm = new HashMap<Integer, Integer>();
int max = 0;
for(int i=0;i<num.length;i++)
{
if(hm.containsKey(num[i]))
continue;
hm.put(num[i],1);
int low = hm.containsKey(num[i]-1)?hm.get(num[i]-1):0;
int high = hm.containsKey(num[i]+1)?hm.get(num[i]+1):0;
int v = low+high+1;
hm.put(num[i]-low,v);
hm.put(num[i]+high,v);
max = Math.max(max,v);
}
return max;
}

[Leetcode][JAVA] 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】Longest Consecutive Sequence

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

  4. Java for LeetCode 128 Longest Consecutive Sequence

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

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

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

  6. 【leetcode】Longest Consecutive Sequence(hard)☆

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

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

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

  8. Leetcode 128. Longest Consecutive Sequence (union find)

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

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

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

随机推荐

  1. XML操作总结

    在开发过程中对XML的使用不是太多,要用到时候也是想办法绕过去,最近一个同事给了一个详细的操作.分享一下 using System; using System.Collections.Generic; ...

  2. python与正则表达式:re模块详解

    re模块是python中处理正在表达式的一个模块 正则表达式知识储备:http://www.cnblogs.com/huamingao/p/6031411.html 1. match(pattern, ...

  3. 碎片事物的提交 commitAllowingStateLoss()

    转:http://blog.csdn.net/kaiqiangzhang001/article/details/42241441 下边两个问题,是在开发中碰到的一些关于Fragment的偶发性的问题, ...

  4. Javascript调用ActiveX示例

      Javascript调用ActiveX示例   写一个ActiveX控件比如叫做MyNameSpace.SecreteInfo,安装在客户机器上,这样可以通过c++获取到机器的几乎任何信息. 在网 ...

  5. [华清远见]FPGA公益培训

    本套视频教程为华清远见 网络公益培训活动,主讲人:姚远老师,华清远见高级讲师. ------------------------------------------------------------ ...

  6. Jsoncpp的使用

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式. 易于人阅读和编写.同时也易于机器解析和生成. 它基于JavaScript Programming Lan ...

  7. Operate blob data in Oracle via C#

    oracle table: CREATE TABLE "SCOTT"."TEST_BLOB"    (    "NAME" VARCHAR2 ...

  8. 驱动开发学习笔记. 0.01 配置arm-linux-gcc 交叉编译器

    驱动开发读书笔记. 0.01 配置arm-linux-gcc 交叉编译器 什么是gcc: 就像windows上的VS 工具,用来编译代码,具体请自己搜索相关资料 怎么用PC机的gcc 和 arm-li ...

  9. c语言知识点

    1.在c语言中,函数,声明,调用的类型务必是一致的, 2.主机id:指ip地址最后一个字节,例如,203.86.61.106,---->106指主机id, 3,端口号:6789,换成16进制1A ...

  10. iOS 设置 文字和 图片的位置

    1.我最开始实现这个采用的方法:重新自定义一个view,然后有两个属性label和imageView,然后设置位置布局,再添加单击手势,用代理回传点击方法. 2.第二种方法:自定义一个Button继承 ...