[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 ...
随机推荐
- IE请求访问的设置
1:问题 目前有个项目采用AngularJs发送Restful风格的请求的方式来实现前后端的通信,测试人员在使用IE浏览器进行测试的时候发现,有时候请求发送不到后台,后来经查找发现是由于IE浏览器的默 ...
- NDK 的开发流程
1.NDK开发所需要的工具 windows 需要在windows下的环境 把c代码打包成 手机能用的函数库 首先模拟手机的环境 1 NDK .sh linux 批处理文件 .bat windows 头 ...
- Android新浪微博获取评论信息、发表评论、转发微博等
首先前面一节中说过,获取用户的微博信息,这里简单介绍下获取微博的评论信息,以及对微博进行评论,转发微博等. OAuth认证,这里就不多说了, 我说名一下接口: 获取微博的评论列表接口: http:// ...
- 动态规划 - 最长递增子序列(LIS)
最长递增子序列是动态规划中经典的问题,详细如下: 在一个已知的序列{a1,a2,...,an}中,取出若干数组组成新的序列{ai1,ai2,...,aim},其中下标i1,i2,...,im保持递增, ...
- C# RSA 算法
RSA公钥加密算法是1977年由Ron Rivest.Adi Shamirh和LenAdleman在(美国麻省理工学院)开发的.RSA取名来自开发他们三者的名字.RSA是目前最有影响力的公钥加密算法, ...
- 使用CodeSmith快速生成映射文件和映射类
一 CodeSmith简介 本文以表自动生成NHibernate的映射文件和映射类的实例来说明一下本软件的使用方法. CodeSmith是一种基于模板的代码生成工具,其使用类似于ASP.NET的语法来 ...
- c++ primer 5th 练习3.43
#include <iostream> using namespace std; int main() { ][]={,,,,,,,,,,,}; /* for(int (&i)[4 ...
- 26、首先通过javascript包的异步加载来学习echarts包的结构
1.在这里先写一写前言,今天在公司搞定了一个对于滚动条进行定位的case,明天开始做TestManagement. 首先大家先来一起看一看流行的javascript文件的加载方式,这里采用别人博客上的 ...
- MC的缓存雪崩现象和缓存无底洞的原因以及导致的后果的总结
缓存雪崩一般是由某个缓存节点失效,导致其他节点的缓存命中率下降,缓存中确实的数据去数据库查询,短时间内,造成数据库服务器的崩溃. 这时,我们需要重启数据库,但重启一段时间后,又会被压垮,但此时缓存的数 ...
- malloc()参数为0的情况
问题来自于<程序员面试宝典(第三版)>第12.2节问题9(这里不评价<程序员面试宝典>,就题论题): 下面的代码片段输出是什么?为什么? char *ptr; ))==NULL ...