当前位置:网站首页>128. Longest continuous sequence
128. Longest continuous sequence
2022-07-24 01:17:00 【Xiao Lu wants to brush the questions】
List of articles
128. The longest continuous sequence
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.
source : Power button (LeetCode)
link :https://leetcode.cn/problems/longest-consecutive-sequence
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Their thinking
Two hash tables
Continuous interval header table + Continuous interval tail table 
100 The process of coming
The header table is added (100,1),1 For the length
Tail table addition (100,1)
Join in 3
Join in 4, Check whether there is an interval that can be connected 
Check the header table , Find out 3, take 3 And 4 Connect , become (3,2), For from 3 set out , It can be connected 2 An interval of length
View footer , Find out 3, take 3 And 4 Connect , become (4,2), Representative to 4 For the end , It can be connected 2 An interval of length
Each time he counts, he builds his own interval , See if it can't match up with before , See if the back can close ,
After you close it strictly every time , You ask me how long the last continuous interval is , Feel free to find - A watch , hold value Take out the maximum value
When every number comes , The operation of hash table is 0(1)
Code
Versions of two hash tables
public static int longestConsecutive2(int[] nums) {
HashMap<Integer, Integer> headMap = new HashMap<Integer, Integer>();
HashMap<Integer, Integer> tailMap = new HashMap<Integer, Integer>();
HashSet<Integer> visited = new HashSet<>();
for (int num : nums) {
if (!visited.contains(num)) {
visited.add(num);
headMap.put(num, 1);
tailMap.put(num, 1);
if (tailMap.containsKey(num - 1)) {
int preLen = tailMap.get(num - 1);
int preHead = num - preLen;
headMap.put(preHead, preLen + 1);
tailMap.put(num, preLen + 1);
headMap.remove(num);
tailMap.remove(num - 1);
}
if (headMap.containsKey(num + 1)) {
int preLen = tailMap.get(num);
int preHead = num - preLen + 1;
int postLen = headMap.get(num + 1);
int postTail = num + postLen;
headMap.put(preHead, preLen + postLen);
tailMap.put(postTail, preLen + postLen);
headMap.remove(num + 1);
tailMap.remove(num);
}
}
}
int ans = 0;
for (int len : headMap.values()) {
ans = Math.max(ans, len);
}
return ans;
}
A hash table
public static int longestConsecutive(int[] nums) {
HashMap<Integer, Integer> map = new HashMap<>();
int len = 0;
for (int num : nums) {
if (!map.containsKey(num)) {
map.put(num, 1);
int preLen = map.containsKey(num - 1) ? map.get(num - 1) : 0;
int posLen = map.containsKey(num + 1) ? map.get(num + 1) : 0;
int all = preLen + posLen + 1;
map.put(num - preLen, all);
map.put(num + posLen, all);
len = Math.max(len, all);
}
}
return len;
}
边栏推荐
- Solve the problem that the double click of Google Chrome browser doesn't respond and can't be started (friendly testing)
- 数仓搭建——ODS层
- LVS load balancing scheduling principle and configuration method
- Tutorial on principles and applications of database system (039) -- MySQL query (I): syntax analysis of select command
- HCIP,OSPF综合实验
- Static extension configuration
- Notes to Chapter 2 of kubernetes in action
- MGRE实验
- 【云原生之kubernetes】kubernetes集群下的Deployment高级资源对象管理
- 好大夫问诊-俞驰-口腔信息
猜你喜欢

Idea setting automatic package import and useless package deletion

ACL——net

Single chip microcomputer learning notes 3 -- single chip microcomputer structure and minimum system (based on Baiwen STM32F103 series tutorials)

Prometheus operator user guide notes

What impact does the European "gas shortage" have on China?

数字化转型时代的企业数据新基建 | 爱分析报告

Linkerd service grid survey notes
![[STM32] basic knowledge of serial communication](/img/0f/7cc59fea9b1edf721c9d06b419896a.png)
[STM32] basic knowledge of serial communication

Static extension configuration

EasyExcel导出案例(只有你想不到)
随机推荐
freemarker
Review questions of polymer synthesis technology
About redis: there is still a risk of data loss after redis sets data persistence
Form resume
Skywalking distributed system application performance monitoring tool - upper
[QNX hypervisor 2.2 user manual]9 VM configuration reference
Idea compiler sets the separation line between methods
创建自签名证书, 对exe文件进行数字签名
JS related knowledge
数字化转型时代的企业数据新基建 | 爱分析报告
Kotlin基础从入门到进阶系列讲解(基础篇)关键字:suspend
Modify node temporarily_ Modules package source code and compile reference to modify dependent packages
Tutorial on the principle and application of database system (044) -- MySQL query (VI): using the limit option to realize paging query
[STM32] basic knowledge of serial communication
Establishment of static route
Notes: middle order traversal of binary trees (three solutions - recursion, iteration, Morris)
C language database: an online dictionary based on TCP multi process, with detailed steps illustrated. Welcome to watch it
Idea setting automatic package import and useless package deletion
The winverifytrust call returned 80096005 error. The timestamp signature or certificate cannot be verified or is damaged
Sublime text 3 汉化+添加常用插件