当前位置:网站首页>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;
}
边栏推荐
- Best used trust automation script (shell)
- 雪崩问题以及sentinel的使用
- Subnet division and subnet summary
- How do I use Google Chrome 11's Upload Folder feature in my own code?
- Redis分布式锁的8大坑
- Chapitre 03 Bar _ Gestion des utilisateurs et des droits
- Leetcode 1482 guess, how about this question?
- LeetCode 128最长连续序列(哈希set)
- The shell script uses two bars to receive external parameters
- Avalanche problem and the use of sentinel
猜你喜欢
深度学习中的随机种子torch.manual_seed(number)、torch.cuda.manual_seed(number)
MySQL knowledge points
How do spark tasks of 10W workers run? (Distributed Computing)
实现pow(x,n)函数
8 pits of redis distributed lock
Edge drawing: a combined real-time edge and segment detector
Hello World generation
So easy deploy program to server
Elk elegant management server log
不用加减乘除实现加法
随机推荐
Detailed explanation of ES6 deconstruction grammar
POI exports excel and displays hierarchically according to parent-child nodes
【读书笔记】《文案变现》——写出有效文案的四个黄金步骤
torch.histc
# 使用 KubeKey 搭建 Kubernetes/KubeSphere 环境的'心路(累)历程'
Introduction to ieda right click source file menu
gcc使用、Makefile总结
md5sum操作
Go tool cli for command line implementation
实现pow(x,n)函数
ctfshow爆破wp
Force buckle - sum of two numbers
Hal library setting STM32 interrupt
文件上传下载
LeetCode 144二叉树的前序遍历、LeetCode 114二叉树展开为链表
Subnet division and subnet summary
串口接收数据方案设计
雪崩问题以及sentinel的使用
[小样本分割]论文解读Prior Guided Feature Enrichment Network for Few-Shot Segmentation
LeetCode 128最长连续序列(哈希set)