当前位置:网站首页>128. 最长连续序列
128. 最长连续序列
2022-07-24 01:04:00 【小卢要刷力扣题】
128. 最长连续序列
给定一个未排序的整数数组 nums ,找出数字连续的最长序列(不要求序列元素在原数组中连续)的长度。
请你设计并实现时间复杂度为 O(n) 的算法解决此问题。
示例 1:
输入:nums = [100,4,200,1,3,2]
输出:4
解释:最长数字连续序列是 [1, 2, 3, 4]。它的长度为 4。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/longest-consecutive-sequence
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解题思路
两个哈希表
连续区间头表+连续区间尾表
100来到的过程
头表加入(100,1),1为长度
尾表加入(100,1)
加入3
加入4,查看有没有能连接的区间
查看头表,发现3,将3与4连起来,变成(3,2),代表从3出发,可以连成2个长度的区间
查看尾表,发现3,将3与4连起来,变成(4,2),代表以4为结尾,可以连成2个长度的区间
每个数来的时候都自己建出自己的区间,看看跟之前都不能合,看看后面能不能合,
你每次都严严格格的合完之后,你问我最后有多长的连续区间,你随便找-张表,把value最大值拿出来
每一个数来到的时候,对于哈希表的操作都是0(1)
代码
两个哈希表的版本
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;
}
一个哈希表
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;
}
边栏推荐
- Understand the locks that can't
- Sword finger offer frog jumps stairs
- 黑马程序员-接口测试-四天学习接口测试-第四天-Postman读取外部数据文件,读取数据文件数据,iHRM项目实战,员工管理模块,添加员工,批量运行测试用例,生成测试报告,
- Sublime text 3 汉化+添加常用插件
- Notes: binary tree pruning (recursion, iteration)
- 【Flyway 介绍】
- About redis: there is still a risk of data loss after redis sets data persistence
- OSI open system interconnection model and tcp/ip model
- Static extension configuration
- [data mining engineer - written examination] Haier company in 2022
猜你喜欢

Memory forensics nssctf otterctf 2018 (replay)

深入理解协程

C language database: an online dictionary based on TCP multi process, with detailed steps illustrated. Welcome to watch it

Intelligent video monitoring solutions for elderly care institutions, using new technologies to help the intelligent supervision of nursing homes

Dark horse programmer - interface test - four day learning interface test - day 4 - postman reads external data files, reads data files, IHRM project practice, employee management module, adds employe

Socket basic knowledge and various usage scenarios

OSI open system interconnection model and tcp/ip model

The salary of a tester who has worked for 3 years after job hopping is twice that of the original. The secret is

Determination of host byte order

落枕如何快速缓解
随机推荐
Concurrent programming 1-2
Tutorial on principles and applications of database system (045) -- MySQL query (VII): aggregate function
Okaleido tiger NFT is about to log in to the binance NFT platform. Are you looking forward to it?
Solve the problem that MySQL inserts Chinese garbled code into the table
Dynamic rip configuration
Hot 100 dynamic programming
Modify node temporarily_ Modules package source code and compile reference to modify dependent packages
Establishment of static route
MySQL exercise: all employees reporting to the CEO
docker redis
[reply] about the fact that I chose the wrong technology at the wrong time
Is flush opening an account risky and safe?
Solve the error: uncaught (in promise) navigationduplicated: avoided redundant navigation to current location:“
Analysis of the advantages of the LAAS scheme of elephant swap led to strong performance of ETOKEN
LVS load balancing scheduling principle and configuration method
Solve the problem that the double click of Google Chrome browser doesn't respond and can't be started (friendly testing)
Basic use of crawler requests module
cnpm 执行时卡住应该怎么解决?
Thread pool summary
OSI open system interconnection model and tcp/ip model