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

边栏推荐
- torch.histc
- 力扣-两数之和
- 伺服第二编码器数值链接到倍福PLC的NC虚拟轴做显示
- leetcode 1482 猜猜看啊,这道题目怎么二分?
- Server rendering technology JSP
- Best used trust automation script (shell)
- [daily training] 1175 Prime permutation
- Thread data sharing and security -threadlocal
- Edge drawing: a combined real-time edge and segment detector
- JS daily development tips (continuous update)
猜你喜欢

How to achieve 0 error (s) and 0 warning (s) in keil5

EtherCAT简介

Redis分布式锁的8大坑

Edlines: a real time line segment detector with a false detection control

倍福TwinCAT3 Ads相关错误详细列表

过滤器 Filter

终极套娃 2.0 | 云原生交付的封装

How do spark tasks of 10W workers run? (Distributed Computing)

Hal library operation STM32 serial port

The 'mental (tiring) process' of building kubernetes/kubesphere environment with kubekey
随机推荐
File upload and download
Chapitre 03 Bar _ Gestion des utilisateurs et des droits
torch.histc
If a parent class defines a parameterless constructor, is it necessary to call super ()?
leetcode 1818 绝对值,排序,二分法,最大值
还在浪费脑细胞自学吗,这份面试笔记绝对是C站天花板
第03章_用戶與權限管理
Subnet division (10)
# 使用 KubeKey 搭建 Kubernetes/KubeSphere 环境的'心路(累)历程'
Edge Drawing: A combined real-time edge and segment detector 翻译
The shell script uses two bars to receive external parameters
Mybati SQL statement printing
完全背包问题
IPv4和IPv6、局域网和广域网、网关、公网IP和私有IP、IP地址、子网掩码、网段、网络号、主机号、网络地址、主机地址以及ip段/数字-如192.168.0.1/24是什么意思?
Server rendering technology JSP
How do I use Google Chrome 11's Upload Folder feature in my own code?
Ultimate dolls 2.0 | encapsulation of cloud native delivery
[小样本分割]论文解读Prior Guided Feature Enrichment Network for Few-Shot Segmentation
倍福TwinCAT3 Ads相关错误详细列表
Leetcode 1482 guess, how about this question?