LeetCode--Longest Consecutive Sequence(最长连续序列) Python
题目描述:
Longest Consecutive Sequence(最长连续序列)
中文:
给定一个未排序的整数数组,找出最长连续序列的长度。
要求算法的时间复杂度为 O(n)。
英文:
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.
Your algorithm should run in O(n) complexity.
class Solution(object):
def longestConsecutive(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
Dict = {}
for i in nums:
Dict[i] = 1
List = Dict.keys()
count_max = 0
while(len(Dict)):
List = Dict.keys()
count = 1
temp_left = List[0]
temp_right = List[0]
Dict.pop(temp_left)
while(1):
if temp_left-1 in Dict and temp_right+1 in Dict:
count = count+2
temp_left = temp_left-1
temp_right = temp_right+1
Dict.pop(temp_left)
Dict.pop(temp_right)
continue
elif temp_left-1 not in Dict and temp_right+1 in Dict:
count = count+1
temp_right = temp_right+1
Dict.pop(temp_right)
continue
elif temp_left-1 in Dict and temp_right+1 not in Dict:
count = count+1
temp_left = temp_left-1
Dict.pop(temp_left)
continue
else:
if count>count_max:
count_max=count
break
else:
break
return count_max
题目来源:力扣
LeetCode--Longest Consecutive Sequence(最长连续序列) Python的更多相关文章
- [Leetcode] Longest consecutive sequence 最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- [leetcode]128. Longest Consecutive Sequence最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Y ...
- 298. Binary Tree Longest Consecutive Sequence最长连续序列
[抄题]: Given a binary tree, find the length of the longest consecutive sequence path. The path refers ...
- 128. Longest Consecutive Sequence最长连续序列
[抄题]: Given an unsorted array of integers, find the length of the longest consecutive elements seque ...
- LeetCode——Longest Consecutive Sequence
LeetCode--Longest Consecutive Sequence Question Given an unsorted array of integers, find the length ...
- [LeetCode] Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- LeetCode: Longest Consecutive Sequence 解题报告
Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...
- [Leetcode] Longest Consecutive Sequence 略详细 (Java)
题目参见这里 https://leetcode.com/problems/longest-consecutive-sequence/ 这个题目我感觉很难,看了半天别人写的答案,才明白个所以然.下面的代 ...
- LeetCode--128--最长连续序列(python)
给定一个未排序的整数数组,找出最长连续序列的长度. 要求算法的时间复杂度为 O(n). 示例: 输入: [100, 4, 200, 1, 3, 2]输出: 4解释: 最长连续序列是 [1, 2, 3, ...
- LeetCode: Longest Consecutive Sequence [128]
[题目] Given an unsorted array of integers, find the length of the longest consecutive elements sequen ...
随机推荐
- Go 数组(2)
把同样类型的一个数组赋值给另外一个数组 package main; import "fmt"; func main() { ] string ; array2:=[]string ...
- jQuery-Ajax请求Json数据并加载在前端页面,附视频教程讲解!
Ajax技术应用广泛,这种异步加载技术,无需刷新网页即可更新网站内容,全局或者局部均可,所以大家应该学会这种技巧,把技术用上来. 创建demo.json文件,用来做数据源: { "t ...
- python之 matplotlib模块之基本三图形(直线,曲线,直方图,饼图)
matplotlib模块是python中一个强大的绘图模块 安装 pip install matplotlib 首先我们来画一个简单的图来感受它的神奇 import numpy as np impo ...
- python3用pygame实现播放音乐文件
import pygameimport time #导入音乐文件file = r'C:\1.wav'pygame.mixer.init()track = pygame.mixer.music.load ...
- Python 中内建属性 __getattribute__
参考自:https://blog.csdn.net/yitiaodashu/article/details/78974596 __getattribute__是属性访问拦截器,就是当这个类的属性被访问 ...
- 【LeetCode 84】柱状图中最大的矩形
题目链接 [题解] 维护一个单调递增的栈. 会发现栈内的第i个元素的前面一个(i-1)元素在原始的序列中的数字 都是要高于第i个元素的.(或者没有元素) 那么第i个元素往左最多可以扩展到第i-1个元素 ...
- 使用C#实现网站用户登录
我们在写灌水机器人.抓资源机器人和Web网游辅助工具的时候第一步要实现的就是用户登录.那么怎么用C#来模拟一个用户的登录拉?要实现用户的登录,那么首先就必须要了解一般网站中是怎么判断用户是否登录的.H ...
- unicode字符集范围
引言 unicode是全世界统一的编码规则,但只规定了各种字符的数字编码(官网:www.unicode.org),具体实现的存储方式有utff-8,utf-16,utf-32等形式,各种形 ...
- Buuctf | BUU LFI COURSE 1
跟着赵师傅学CTF,这里是我的学习记录 ?file=/flag ?file=/var/log/nginx/access.log :包含ngnix的日志记录 在user-agent里面插入 :bbbbb ...
- git add 添加错文件的撤销方法
git add 添加 多余文件 这样的错误是由于,有的时候 可能 git add . (空格+ 点) 表示当前目录所有文件,不小心就会提交其他文件 git add 如果添加了错误的文件的话 撤销操作 ...