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 ...
随机推荐
- Move Over and Click Link
Move Over and Click Link [Documentation] 等待悬浮菜单中的元素出现并单击元素 [Arguments] ${hover_locator} ${opt_locato ...
- redis开发规范阿里云
一.键值设计 1.key名设计 1) 可读性和可管理性: 以业务名或数据库名为前缀,以防key冲突,用冒号分隔,比如业务名:表名:ID 2)简洁性: 保证语义的前提下,控制key的长度,当key较多 ...
- H2database创建表
语法和sql server大同小异 create table users(id int primary key not null int identity, name varchar(20))
- Python内建模块--collections
python内建模块--collections collections是Python内建的一个集合模块,提供了许多有用的集合类. namedtuple 我们知道tuple可以表示不变集合,例如,一个点 ...
- LDD快速参考
第二章 快速参考 本节中出现的条目会以它们在文中出现的顺序列出: insmod modprobe rmmod 用来装载模块到正运行的内核和移除模块的用户空间工具: #include <linux ...
- AngularJS ng-model 指令
AngularJS ng-model 指令 ng-model 指令用于绑定应用程序数据到 HTML 控制器(input, select, textarea)的值. 代码示例如下: <!DOCTY ...
- JavaScript实现Word、Excel、PPT在线预览
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/qq_27626333/article/d ...
- linux记事工具:RedNotebook Lifeograph Kontact ThotKeeper
Linux桌面有许多灵活而功能强大的日记工具,如支持标签.加密.多种日志模版和实时搜索.其中的优秀者包括: RedNotebook Lifeograph Kontact ThotKeeper
- JAVA 大数开方模板
JAVA 大数开方模板 import java.math.BigInteger; import java.math.*; import java.math.BigInteger; import jav ...
- 关于之前提到的python开发restful风格的接口
此处不做详细说明. https://gitee.com/alin2017/my-i-demo.git 附上git地址,有兴趣的可以去clone一下. 里面针对代码都有相应的注释, 对于每一个文件也有r ...