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. listview java.lang.ArrayIndexOutOfBoundsException:

    检测下BaseAdapter  下的getViewTypeCount()方法返回的值与getItemViewType返回的个数是否是相等的!

  2. U盘分区之后如何恢复

    操作步骤: 1.插入U盘. 2.按windows键,右键点击“运行”,再左键点击以管理员身份运行. 3.输入diskpart,按enter. 4.输入list disk,按enter. 5.之后会看到 ...

  3. MVC 自定义IModelBinder实现json参数转Dictionary<string, string>

    IModelBinder的学习不算深入,现在用它来实现一个json转Dictionary<string, string> 一.原始json转Dictionary<string, st ...

  4. Android开发--微信布局(ListView)基本运用

    ListView 1.ListVeiw 用来展示列表的View. 2.适配器 用来把数据映射到ListView上的中介. 3.数据    具体的将被映射的字符串,图片,或者基本组件. 根据列表的适配器 ...

  5. 单机运行环境搭建之 --CentOS-6.5安装配置Tengine

    一.安装pcre:   cd /usr/local/src wget http://downloads.sourceforge.net/project/pcre/pcre/8.34/pcre-8.34 ...

  6. ftp服务器远程拷贝命令

    xiamense@xiamense-testserver:~$ ftp 218.5.82.40 输入账户密码 get 远程文件路径 本机服务器路径get pa20160927.xml /home/xi ...

  7. C++连接mysql的两种方式(ADO连接和mysql api连接)

    一.ADO连接mysql 1.安装mysql-5.5.20-win32.msi和mysql-connector-odbc-5.3.4-win32.msi(一般两个安装程序要匹配,否则可能连接不上)  ...

  8. Explode TArray

    function Explode(const Separator, S: string; Limit: Integer = 0): TArray;var SepLen : Integer; F, P ...

  9. LC.exe已退出,代码为-1错误

    因为证书的原因,把项目中“properties”目录下的“license.licx”文件删除,再编译就成功了.如图:

  10. SAP无损耗,FP前台和回写均有2%损耗

    SAP前台显示无损耗 FP前台显示有损耗 回写也有损耗 检查:从SAP取数到FP表是没有损耗 1132物料编码的主数据也是没有损耗 检查:FP_MO2SAP存储过程