Longest Consecutive Sequence leetcode java
题目:
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.
题解:
这道题利用HashSet的唯一性解决,能使时间复杂度达到O(n)。首先先把所有num值放入HashSet,然后遍历整个数组,如果HashSet中存在该值,就先向下找到边界,找的同时把找到的值一个一个从set中删去,然后再向上找边界,同样要把找到的值都从set中删掉。所以每个元素最多会被遍历两边,时间复杂度为O(n)。
代码如下:
1 public int longestConsecutive(int[] num) {
2 if(num == null||num.length == 0)
3 return 0;
4
5 HashSet<Integer> hs = new HashSet<Integer>();
6
7 for (int i = 0 ;i<num.length; i++)
8 hs.add(num[i]);
9
int max = 0;
for(int i=0; i<num.length; i++){
if(hs.contains(num[i])){
int count = 1;
hs.remove(num[i]);
int low = num[i] - 1;
while(hs.contains(low)){
hs.remove(low);
low--;
count++;
}
int high = num[i] + 1;
while(hs.contains(high)){
hs.remove(high);
high++;
count++;
}
max = Math.max(max, count);
}
}
return max;
}
Longest Consecutive Sequence leetcode java的更多相关文章
- 128. Longest Consecutive Sequence(leetcode)
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- Binary Tree Longest Consecutive Sequence -- LeetCode
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- Longest Consecutive Sequence [LeetCode]
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- Longest Consecutive Sequence——Leetcode
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- [LeetCode] Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- LeetCode Binary Tree Longest Consecutive Sequence
原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/ 题目: Given a binary t ...
- LeetCode 549. Binary Tree Longest Consecutive Sequence II
原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence-ii/description/ 题目: G ...
- LeetCode 298. Binary Tree Longest Consecutive Sequence
原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/ 题目: Given a binary t ...
- [LeetCode] 128. Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
随机推荐
- 牛客练习赛3 F - 监视任务
链接:https://www.nowcoder.net/acm/contest/13/F来源:牛客网 题目描述
- 使用easypoi导出excel
EasyPOI是在jeecg的poi模块基础上,继续开发独立出来的,可以说是2.0版本,EasyPoi封装的目的和jeecg一致,争取让大家write less do more ,在这个思路上easy ...
- C++运算符重载 模板友元 new delete ++ = +=
今天的重载是基于C++ 类模板的,如果需要非类模板的重载的朋友可以把类模板拿掉,同样可以参考,谢谢. 一.类模板中的友元重载 本人喜好类声明与类成员实现分开写的代码风格,如若您喜欢将类成员函数的实现写 ...
- BZOJ 2302: [HAOI2011]Problem c [DP 组合计数]
2302: [HAOI2011]Problem c Time Limit: 30 Sec Memory Limit: 256 MBSubmit: 648 Solved: 355[Submit][S ...
- SPOJ7586 NUMOFPAL manacher算法
题目大意: 求一个串中有多少个回文子串 这..... 妥妥的模板题吧.... 对所有的$r[i] / 2$进行求和即可,其中,$r[i]$为以$i$为中心的回文半径 $r[i] / 2$怎么来的,画下 ...
- CF961E Tufurama【主席树】
CF961E Tufurama 题意翻译 题目描述 有一天Polycarp决定重看他最喜爱的电视剧<Tufurama>.当他搜索“在线全高清免费观看Tufurama第3季第7集”却只得到第 ...
- Codechef December Challenge 2014 Chef and Apple Trees 水题
Chef and Apple Trees Chef loves to prepare delicious dishes. This time, Chef has decided to prepare ...
- python—第三库的安装方法
Windows系统下安装第三方Python库的三种方法: 1.使用easy_install命令安装 一般在安装完Python后再C:\Python27\Scripts 目录下有 easy_instal ...
- CentOS 7 下编译安装lnmp之nginx篇详解
一.安装环境 宿主机=> win7,虚拟机 centos => 系统版本:CentOS Linux release 7.5.1804 (Core),ip地址 192.168.1.168 ...
- ThinkPHP实现登录限制时__construct和_initialize的区别
ThinkPHP支持两种构造方法: __construct和_initialize(ThinkPHP内置的构造方法). 测试URL为: http://oa.com/index.php/Admin/ ...