当前位置:网站首页>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;
}

边栏推荐
猜你喜欢

Take you through a circuit board, from design to production (dry goods)

Ultimate dolls 2.0 | encapsulation of cloud native delivery

Nacos

最好用的信任关系自动化脚本(shell)

File upload and download

LeetCode 144二叉树的前序遍历、LeetCode 114二叉树展开为链表

Ctfshow blasting WP

Design practice of current limiting components

Introduction to EtherCAT

Research on target recognition and tracking based on 3D laser point cloud
随机推荐
Mybati SQL statement printing
Take you through a circuit board, from design to production (dry goods)
Subnet division (10)
伺服第二编码器数值链接到倍福PLC的NC虚拟轴做显示
雪崩问题以及sentinel的使用
别再说不会解决 “跨域“ 问题啦
Detailed explanation of ES6 deconstruction grammar
串口接收数据方案设计
[小样本分割]论文解读Prior Guided Feature Enrichment Network for Few-Shot Segmentation
4、【WebGIS实战】软件操作篇——数据导入及处理
完全背包问题
If a parent class defines a parameterless constructor, is it necessary to call super ()?
Basic concept and classification of sorting
The shell script uses two bars to receive external parameters
Clion and C language
Edge drawing: a combined real-time edge and segment detector
Ctfshow blasting WP
BluePrism注册下载并安装-RPA第一章
LeetCode 144二叉树的前序遍历、LeetCode 114二叉树展开为链表
Basic concepts of database