[Leetcode][JAVA] Longest Consecutive Sequence
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的更多相关文章
- [LeetCode] 128. Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- [LeetCode] 128. Longest Consecutive Sequence 解题思路
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- 【leetcode】Longest Consecutive Sequence
Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...
- Java for LeetCode 128 Longest Consecutive Sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- leetcode 128. Longest Consecutive Sequence ----- java
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- 【leetcode】Longest Consecutive Sequence(hard)☆
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- [leetcode]128. Longest Consecutive Sequence最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Y ...
- Leetcode 128. Longest Consecutive Sequence (union find)
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Y ...
- LeetCode 128 Longest Consecutive Sequence 一个无序整数数组中找到最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.Fo ...
随机推荐
- QTdebug时没有调试引擎
问题描述: 在调试程序时,点击调试按钮,弹出no engine. 问题解决: 到官网下载调试的SDK.https://developer.microsoft.com/zh-cn/windows/dow ...
- dispay属性的block,inline,inline-block
转自下面的几位大神: http://www.cnblogs.com/KeithWang/p/3139517.html 总体概念 block和inline这两个概念是简略的说法,完整确切的说应该是 bl ...
- xpath表达式,提取标签下的全部内容(将其他标签过滤)
例如要提取span下的内容 //div[@class="content"]/span 正确的其中一种写法如下data = response.xpath('//div[@class= ...
- 第五百八十一天 how can I 坚持
也是醉了,现在买个手机都特么搞饥饿营销,吹牛B就要付出吹牛B的代价,哎,好伤感. 晚上学习也没学好.感觉人和人之间的信任怎么都没了呢..但愿是我想多了,其实就是我想多了,以后说话还是要多注意. 睡觉吧 ...
- Ubuntu上安装MongoDB(译)
add by zhj:直接从第四步开始就可以了,而且安装好MongoDB后会自动启动的,不必自己去执行启动命令 原文:https://docs.mongodb.com/manual/tutorial/ ...
- OpenSSL命令系列
1.1 ssl命令系列前言 openssl命令的格式是"openssl command command-options args",command部分有很多种命令,这些命令需要依赖 ...
- bash脚本编程之二 字符串测试及for循环
shell中大量的测试和比较选项而困惑呢? 这个技巧可以帮助您解密不同类型的文件.算术和字符串测试,这样您就能够知道什么时候使用 test. [ ]. [[ ]].(( )) 或 if-then-el ...
- PHP生成静态页
代码如下: <? function makedir($mudir) //创建目录 { $file = "./$mudir"; @mkdir($file,07 ...
- Jquery Mobile 动态添加元素然后刷新 转
Jquery Mobile 动态添加元素然后刷新 (2013-05-09 12:39:05) 转载▼ 标签: it 分类: Mobile jquery 表单元素每一个元素都有自己刷新的方法,每当改变它 ...
- css3的2D转换
CSS3的2D转换用transform来实现 1.rotate() /*通过 rotate() 方法,元素顺时针旋转给定的角度.允许负值,元素将逆时针旋转.*/ 2.scal() /*通过 s ...