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 ...
随机推荐
- 在centOS使用systemctl配置启动多个tomcat
公司服务器使用的是阿里云CentOS7,CentOS7和CentOS6目前最大区别就是service变成了现在的systemctl,简单的查了一下并结合使用,发现systemctl功能上等同于6上面的 ...
- CSS3组件化之ios版菊花loading
<div class="juhua-loading"> <div class="jh-circle1 jh-circle-ios">&l ...
- 【CF 453A】 A. Little Pony and Expected Maximum(期望、快速幂)
A. Little Pony and Expected Maximum time limit per test 1 second memory limit per test 256 megabytes ...
- Struts2 (下)
接收参数 当发送一个请求时,除了使用RequestApi来接收参数之外,Struts2内部提供了3种接收参数的方式 接收参数的方式 1. 提供属性set方法的方式 在Action当中提供对应属性的se ...
- 【字符串哈希】The 16th UESTC Programming Contest Preliminary F - Zero One Problem
题意:给你一个零一矩阵,q次询问,每次给你两个长宽相同的子矩阵,问你它们是恰好有一位不同,还是完全相同,还是有多于一位不同. 对每行分别哈希,先一行一行地尝试匹配,如果恰好发现有一行无法对应,再对那一 ...
- 【平面图最小割】BZOJ1001- [BeiJing2006]狼抓兔子
[题目大意]左上角点为(1,1),右下角点为(N,M)(上图中N=4,M=5).有以下三种类型的道路 1:(x,y)<==>(x+1,y) 2:(x,y)<==>(x,y+1) ...
- eclipse转idea, 快捷键设置
设置快捷键的途径: 打开idea的配置,找到Keymap,设置为eclipse 另外还要手动设置某些快捷键 上下移动 点击类打开 代码提示 查询 重命名 快速实现接口 回到上一次光标处
- Codeforces Round #288 (Div. 2) E. Arthur and Brackets 贪心
E. Arthur and Brackets time limit per test 2 seconds memory limit per test 128 megabytes input stand ...
- Codeforces Round 486C - Palindrome Transformation 贪心
C. Palindrome Transformation time limit per test 1 second memory limit per test 256 megabytes input ...
- FolderSync文件夹同步
FolderSync是一款支持各大国外网盘同步的软件,目前支持 SkyDrive, Dropbox, SugarSync, Ubuntu One, Box.net, LiveDrive, HiDriv ...