[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 ...
随机推荐
- noip2006解题报告
T1.能量项链 给出一串数字(其实是个环也就是可以旋转).n个数组成n颗珠子,形如: 1 2 3 4 表示的珠子是(1,2)(2,3)(3,4)(4,1) 定义珠子的聚合:如前两颗聚合放出能量为1*2 ...
- MySQL 常用命令(持续更新)
停止启动MySQL服务 停止:net stop mysql启动:net start mysql 查看正在运行的线程 SHOW PROCESSLIST SHOW FULL PROCESSLIST 杀死线 ...
- java中的类型比较
Java 里的既可以比较基本类型也可以比较引用类型. 对于基本类型,Java 的==比较值比较 对于引用类型,Java 的==比较了引用的是否为同一个对象(比较内存地址), 也就是说这两个变量是否都指 ...
- PIC32MZ tutorial -- Blinky LED
Today I finish the "Blinky LED" application on PIC32MZ starter kit. This application let L ...
- redis基础使用
redis分linux,window两个版本分支. redis在window下的使用先下载相关包.下载地址:https://github.com/MSOpenTech/redis/releases 下 ...
- C# 判断是否联网
public static class Internet { [DllImport("winInet.dll")] private static extern bool Inter ...
- HttpClient请求发送的几种用法:
/// <summary> /// HttpClient实现Post请求 /// </summary> static async void dooPost() { string ...
- easyul获取各种属性ID 和赋值
//span赋值 $('#state1').text("审核通过"); //textarea赋值 $("#memo").val(''); //隐藏域 $(&q ...
- AngularJS学习--- AngularJS中模板链接和图像 ng-src step6
接上一篇文章,本文将主要介绍angularjs中的模板链接,和图像显示? 首先,切换分支,启动项目: git checkout step- npm start 1.效果 相较于前一篇文章,明显感觉多了 ...
- ADB理解
在做手机测试时候,经常用到的命令就是adb.如adb shell,adb devices,adb logcat等等 那么什么是adb,怎么用呢? 一.adb adb的全称为Android Debug ...