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 ...
随机推荐
- Logback配置解析
logback优点 比较吸引的几个优点如下: 内核重写,初始化内存加载更小 文档比较齐全 支持自动重新加载配置文件,扫描过程快且安全,它并不需要另外创建一个扫描线程 支持自动去除旧的日志文件,可以控制 ...
- TRUNCATE can't with condition
No, TRUNCATE is all or nothing. You can do a DELETE FROM <table> WHERE <conditions> but ...
- tftp协议
<前言> 嵌入式开发是一个交叉开发的模式,需要将宿主机上的文件烧写到目标机上. 方式: JTAG USB 串口 网络 <tftp下载> 首先需要将宿主机架成一个TFTP的服务器 ...
- C++下的强制转换类型
一.static_cast static_cast,静态类型转换. 下面,我举几个例子,大家就能清楚了. int main(int argc, char const *argv[]) { char ...
- WinForm 使用 NPOI 2.2.1从datatable导出Excel
最新的NOPI应该是2.3了,但在官网上还是2.2.1. 也是第一次使用NPOI来导出Excel文件. 在写的时候搜不到2.2.1的教程,搜了一个2.2.0的教程. 不过也没什么问题,NPOI是真的方 ...
- 大型系统中使用JMS优化技巧–Sun OpenMQ
我们先来看看在Sun OpenMQ系统中 一个持久.可靠的方式传送消息的步骤是怎么样的,如图所示: 查看大图请点击这里 在传送过程中,系统处理JMS消息分为以下两类: ■ 有效负荷消息,由生成方发 ...
- ArrayList,Vector,LinkedList
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha 数组列表 和 向量Vector, 都是使用数组方式存储. 向量 使用了 同步synchr ...
- 洛谷.3803.[模板]多项式乘法(NTT)
题目链接:洛谷.LOJ. 为什么和那些差那么多啊.. 在这里记一下原根 Definition 阶 若\(a,p\)互质,且\(p>1\),我们称使\(a^n\equiv 1\ (mod\ p)\ ...
- HDU 5150 Sum Sum Sum 素数
Sum Sum Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- MySQL数据库基准压力测试工具之MySQLSlap使用实例
一.Mysqlslap介绍 mysqlslap是MySQL5.1之后自带的benchmark基准测试工具,类似Apache Bench负载产生工具,生成schema,装载数据,执行benckmark和 ...