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. AX 2012 EP服务器配置多个环境

    AX 2012 如何在一台服务器配置不同环境的EP站点 安装完EP后,修改对应站点的web.config文件,指定需要连接的客户端配置文件路径即可,如下图: ` ``````````````````` ...

  2. ORCLE数据库导出导入

    从一个用户导出用户所有结构数据 再导入另一个用户里面 ORACLE导出用户下的数据库  exp 命令 用户名/密码服务名 文件地址  owner=(用户名)exp COM_HIOSC_OLD/COM_ ...

  3. 4,SFDC 管理员篇 - 数据模型 - 基本对象

    Setup | Customize | Object Name | Filed   1, 标准字段定义 standard field:系统字段,不能删除,但是能在页面中remove non-requi ...

  4. Spark Streaming源码解读之Job动态生成和深度思考

    本期内容 : Spark Streaming Job生成深度思考 Spark Streaming Job生成源码解析 Spark Core中的Job就是一个运行的作业,就是具体做的某一件事,这里的JO ...

  5. 做一个高效的IOS开发工程师

    最近觉得自己的开发效率太慢了,总结了一下:熟练度不够是一方面,经常用到东西查看一下,积累问题?一方面,这个无法分享的.现在主要分享的是:如何高效的用好自己的时间. 1.善用xcode. xcode实在 ...

  6. json格式的时间转换

    //yyyy-MM-dd HH:mm:SS function JsonDateToDate(jsondate) { var date = new Date(parseInt(jsondate.repl ...

  7. dedecms 打印出网站所有 文章标题和链接(URL)的方法

    {dede:arclist row="100000"} <li>[field:fulltitle/]***网站URL地址***[field:arcurl/]</l ...

  8. VS2013使用EF6连接MySql

    前提:a.安装MySql的VS插件(版本请下载最新版) 我用的是:mysql-for-visualstudio-1.1.4 b.安装用于.net连接程序  mysql-connector-net-6. ...

  9. solrCloud 管理

    创建collection: /soft/server/solr-4.10.0/example/scripts/cloud-scripts/zkcli.sh -cmd upconfig -zkhost ...

  10. 使用CocoaPods配置工程

    1.首先搭建环境,配置CocoaPods,具体请参考 http://code4app.com/article/cocoapods-install-usage 2.打开终端,输入 cd 空格 把工程拖入 ...