当前位置:网站首页>Leetcode 128 longest continuous sequence (hash set)
Leetcode 128 longest continuous sequence (hash set)
2022-07-01 03:29:00 【It's seventh uncle】
Top1:LeetCode 128 The longest continuous sequence ( Hash set)【 There can be duplicate numbers in the array , Big that is able to use set Weight removal does not affect 】
Title Description :
Given an array of unsorted integers nums , Find the longest sequence of consecutive numbers ( Sequence elements are not required to be contiguous in the original array ) The length of .
Please design and implement it with a time complexity of O(n) The algorithm to solve this problem .
Example 1:
Input :nums = [100,4,200,1,3,2]
Output :4
explain : The longest consecutive sequence of numbers is [1, 2, 3, 4]. Its length is 4.
Example 2:
Input :nums = [0,3,7,2,5,8,4,6,0,1]
Output :9
One 、 Consider enumerating arrays into set after for(int num : set) Traverse , From every one x Start enumeration set Whether or not contains(x+1) Record length ; But here we have to go , That is, when traversing x There is x-1 stay set In the middle of the day , skip
Two 、 Use one current Store the traversed current num Flow length of , Again longest = Math.max(longest, current); Store the longest answer
De duplication skip details reference leetcode Slide map in the source page : The longest continuous sequence - Hashtable 
Here, save the array to set And traversal code :
Set<Integer> set = new HashSet<>();
for (int num : nums) {
// duplicate removal , And the time complexity of the query is O(1)
set.add(num);
}
int longest = 0; // Initialize the final return value , Get into for After the cycle longest = Math.max(longest, current); to update
for (int num : set) {
} // Traverse set Hash set // for Every one in the loop num It's all new , And the last time num No conflict , It's similar to a local variable
You can use the complete code
public static int longestConsecutive2(int[] nums) {
Set<Integer> set = new HashSet<>();
for (int num : nums) {
// duplicate removal , And the time complexity of the query is O(1)
set.add(num);
}
int longest = 0; // Initialize the final return value , Get into for After the cycle longest = Math.max(longest, current); to update
for (int num : set) {
// Traverse set Hash set // for Every one in the loop num It's all new , And the last time num No conflict , It's similar to a local variable
if (!set.contains(num - 1)) {
// int curNum = num; // Store the current num, Into the back of while loop
int current = 1; // to num The stream length becomes 1
while (set.contains(num + 1)) {
num += 1;
current += 1;
}
longest = Math.max(longest, current);
}
}
return longest;
}

边栏推荐
- C语言多线程编程入门学习笔记
- JUC learning
- If a parent class defines a parameterless constructor, is it necessary to call super ()?
- How the network is connected: Chapter 2 (Part 2) packet receiving and sending operations between IP and Ethernet
- Elk elegant management server log
- JS日常开发小技巧(持续更新)
- 伺服第二编码器数值链接到倍福PLC的NC虚拟轴做显示
- 多元线性回归
- Introduction to the core functions of webrtc -- an article to understand peerconnectionfactoryinterface rtcconfiguration peerconnectioninterface
- leetcode 1482 猜猜看啊,这道题目怎么二分?
猜你喜欢
随机推荐
Include() of array
Ctfshow blasting WP
5、【WebGIS实战】软件操作篇——服务发布及权限管理
终极套娃 2.0 | 云原生交付的封装
LeetCode 128最长连续序列(哈希set)
网页不能右键 F12 查看源代码解决方案
8 pits of redis distributed lock
监听器 Listener
Druid监控统计数据源
Feign remote call and getaway gateway
ECMAScript 6.0
Pyramid Scene Parsing Network【PSPNet】论文阅读
leetcode 1818 绝对值,排序,二分法,最大值
split(),splice(),slice()傻傻分不清楚?
JUC学习
How do I use Google Chrome 11's Upload Folder feature in my own code?
如何校验两个文件内容是否相同
Common interview questions for performance test
Introduction to webrtc concept -- an article on understanding source, track, sink and mediastream
限流组件设计实战






