当前位置:网站首页>【LeetCode】128. Longest continuous sequence
【LeetCode】128. Longest continuous sequence
2022-06-10 01:33:00 【LawsonAbs】
0. summary
- If you want to reduce the time complexity , Then you may need to use large memory ( Increase space complexity ).
- 【 A sequence has only one starting point 】
1. subject
2. thought
This is a difficult question to think about . The difficulty lies in , How to ensure that O(n) Time complexity of ? We can set up a dictionary to store list Numbers that have appeared . When you come across a number num When , Maybe it's a number in a sequence , It could also be a single number .【 But we know that a sequence has only one starting point 】, So if the number -1 (num-1) In the assembly , Then skip the number , Because it is not the starting point of this sequence . If it's not in the collection , So assignment res=1.
To be specific :
- (1) Define a set aggregate . Used to indicate the current list Number of existing
- (2) Sequential traversal list The number in , At the same time judge num-1( num=nums[i]) Whether in set in , If in , then continue To the next , Otherwise, judgment num+1 Whether in set in , Update results
3. Code
class Solution:
def longestConsecutive(self, nums: List[int]) -> int:
a = set(nums)
res = 0
for i in range(len(nums)):
cur_num = nums[i]
if cur_num - 1 in a: # If num-1 There is also this list in , Then directly continue
continue
else: # Start sequential search
cnt = 0
while(cur_num in a):
cnt+=1
cur_num+=1
res = max(res,cnt)
return res
边栏推荐
- Unity technology - 2D project experience
- ESRI integrates Lightbox data to extend geocoding in Canada
- 缓解修复Android Studio卡顿,Kotlin代码提示慢
- You're not still using xshell, are you? This open source terminal tool is yyds!
- Curriculum Learning and Graph Neural Networks (or Graph Structure Learning)
- 【LeetCode】接雨水
- [learn FPGA programming from scratch -16]: quick start chapter - operation steps 2-4- basic syntax of Verilog HDL language description language (both software programmers and hardware engineers can un
- Graduated in 985, failed to start a business at the age of 35, returned at the age of 36, and was laid off at the age of 40. What should middle-aged couples do if they are unemployed?
- 生产线往越南转移未必是好选择,三星手机已受累并计划回归韩国
- Why do programmers run away after two years?
猜你喜欢

What are the good techniques for domestic spot silver: simple usage of common indicators

Source code analysis of Tencent libco collaboration open source library (I) -- download libco compilation and installation and try to run the sample code

IDEA 版 Postman问世,亲测好用
[email protected] 项目实训"/>[email protected] 项目实训

The IPO meeting of xinghuan science and Technology Innovation Board: the gross profit margin remains at a high level, with Tencent and other major shareholders

为什么芯片设计也需要「匠人精神」?

How to implement complex SQL such as distributed database sub query and join?

Where should middle-aged test development engineers go? The unknown is tomorrow, break the label

How can I right-click win11 to open all options directly?

孙宇晨等收购Poloniex,公链交易所双轮驱动波场生态
随机推荐
物联网工程设计与实施
【Multisim仿真】差分比例放大电路
孙宇晨等收购Poloniex,公链交易所双轮驱动波场生态
电脑系统怎么修改图片格式
你不会还在用Xshell吧?这款开源的终端工具才是yyds!
Where is safe and reliable for Hangzhou futures to open an account?
【LeetCode】221.最大正方形
Experimental three character type and its operation (New)
Solution to the problem of the greatest common divisor and the least common multiple of Luogu p1029
CocosCreator旧活新整-合成大粽子
生产线往越南转移未必是好选择,三星手机已受累并计划回归韩国
Various utilization forms of map tile data and browsing display of tile data
Locust:微服务性能测试利器
Trichview and scalericview set default Chinese
【LeetCode】437.路径总和III
[從零開始學習FPGA編程-16]:快速入門篇 - 操作步驟2-4- Verilog HDL語言描述語言基本語法(軟件程序員和硬件工程師都能看懂)
Miniature current display
1265_FreeRTOS中向任务就绪链表增加任务的实现分析
0 is it feasible to conduct software testing only by self-study? After reading this article, I am not confused
[neural network] (22) convmixer code reproduction, network analysis, complete tensorflow code attached