Longest Consecutive Sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.
Your algorithm should run in O(n) complexity.
Given [100, 4, 200, 1, 3, 2],
The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4.
分析:
既然要求O(n) complexity,排序这种方法就放弃了。这里用了HashSet来保存每个数,然后从这个数开始,左右寻找是否有相邻的数。非常有新意的方法。
public class Solution {
public int longestConsecutive(int[] num) {
if (num == null || num.length == ) return ;
HashSet<Integer> hs = new HashSet<Integer>();
for (int i = ; i < num.length; i++) {
hs.add(num[i]);
}
int max = ;
for (int i = ; i < num.length; i++) {
if (hs.contains(num[i])) {
int count = ;
hs.remove(num[i]);
int low = num[i] - ;
while (hs.contains(low)) {
hs.remove(low);
low--;
count++;
}
int high = num[i] + ;
while (hs.contains(high)) {
hs.remove(high);
high++;
count++;
}
max = Math.max(max, count);
}
}
return max;
}
}
参考请注明出处:cnblogs.com/beiyeqingteng/
Longest Consecutive Sequence的更多相关文章
- [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- [LeetCode] Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- Binary Tree Longest Consecutive Sequence
Given a binary tree, find the length of the longest consecutive sequence path (连续的路径,不是从小到大). The pa ...
- 128. Longest Consecutive Sequence(leetcode)
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- [LintCode] Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. H ...
- LeetCode Binary Tree Longest Consecutive Sequence
原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/ 题目: Given a binary t ...
- 24. Longest Consecutive Sequence
Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...
- 【leetcode】Longest Consecutive Sequence
Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...
- 【LeetCode OJ】Longest Consecutive Sequence
Problem Link: http://oj.leetcode.com/problems/longest-consecutive-sequence/ This problem is a classi ...
- 298. Binary Tree Longest Consecutive Sequence
题目: Given a binary tree, find the length of the longest consecutive sequence path. The path refers t ...
随机推荐
- shiro 与 web 的结合
本次使用的jar包为 shiro-core-.jar shiro-web-.jar 从Shiro 1.2开始引入了Environment/WebEnvironment的概念,即由它们的实现提供相应的S ...
- 前端筑基篇(一)->ajax跨域原理以及解决方案
说明 跨域主要是由于浏览器的“同源策略”引起,分为多种类型,本文主要探讨Ajax请求跨域问题 前言 参考来源 什么是跨域 ajax跨域的表现 跨域的原理 如何解决跨域问题 JSONP方式解决跨域问题 ...
- 处理Json数据中的日期类型.如/Date(1415169703000)/格式
在asp.net mvc后台返回到视图中的json数据中想对数据进行操作,发现日期类型无法直接进行操作,需要转换为指定格式才行.在网上也搜了下方法也很多,觉得有点麻烦,最终使用正则搞定了,分享下: v ...
- @SuppressWarnings含义
J2SE 提供的最后一个批注是 @SuppressWarnings.该批注的作用是给编译器一条指令,告诉它对被批注的代码元素内部的某些警告保持静默. @SuppressWarnings 批注允许您选择 ...
- springMVC实现防止重复提交
参考文档:http://my.oschina.net/mushui/blog/143397
- 最新版本的DBCP数据源配置
弄了我一下午,把该加的包都加进去了还是没用,后来把DBCP的包打开来看看才发现路径不对.配置如下: <!-- 使用dbcp配置数据源 --> <bean id="dataS ...
- Ubuntu学习总结-01 用VMware 8安装Ubuntu 12.04详细过程
1 Ubuntu 下载地址 http://www.ubuntu.com/download/desktop 2 安装Ubuntu 转载用VMware 8安装Ubuntu 12.04详细过程 http:/ ...
- aapt aidl
AIDL:Android Interface Definition Language,即Android接口定义语言 aapt即Android Asset Packaging Tool,在SDK的bui ...
- 解决Surface Pro外接移动硬盘经常睡眠的问题
1. 打开注册表,找到下面的键 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Power\PowerSettings\0012ee47-904 ...
- POP3,全名为“Post Office Protocol - Version 3”,即“邮局协议版本3”
POP3 锁定 本词条由“科普中国”百科科学词条编写与应用工作项目 审核 . POP3,全名为“Post Office Protocol - Version 3”,即“邮局协议版本3”.是TCP/IP ...