题目描述:

  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的更多相关文章

  1. [Leetcode] Longest consecutive sequence 最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  2. [leetcode]128. Longest Consecutive Sequence最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Y ...

  3. 298. Binary Tree Longest Consecutive Sequence最长连续序列

    [抄题]: Given a binary tree, find the length of the longest consecutive sequence path. The path refers ...

  4. 128. Longest Consecutive Sequence最长连续序列

    [抄题]: Given an unsorted array of integers, find the length of the longest consecutive elements seque ...

  5. LeetCode——Longest Consecutive Sequence

    LeetCode--Longest Consecutive Sequence Question Given an unsorted array of integers, find the length ...

  6. [LeetCode] Longest Consecutive Sequence 求最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  7. LeetCode: Longest Consecutive Sequence 解题报告

    Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...

  8. [Leetcode] Longest Consecutive Sequence 略详细 (Java)

    题目参见这里 https://leetcode.com/problems/longest-consecutive-sequence/ 这个题目我感觉很难,看了半天别人写的答案,才明白个所以然.下面的代 ...

  9. LeetCode--128--最长连续序列(python)

    给定一个未排序的整数数组,找出最长连续序列的长度. 要求算法的时间复杂度为 O(n). 示例: 输入: [100, 4, 200, 1, 3, 2]输出: 4解释: 最长连续序列是 [1, 2, 3, ...

  10. LeetCode: Longest Consecutive Sequence [128]

    [题目] Given an unsorted array of integers, find the length of the longest consecutive elements sequen ...

随机推荐

  1. Flutter-RaisedButton

    RaisedButton({ Key key, //点击按钮的回调出发事件 @required VoidCallback onPressed, //水波纹高亮变化回调 ValueChanged< ...

  2. matlab中求解线性方程组的rref函数

    摘自:http://www.maybe520.net/blog/987/ matlab中怎么求解线性方程组呢? matlab中求解线性方程组可应用克拉默法则(Cramer's Rule)即通过det( ...

  3. nyoj 1022:合纵连横(并查集删点)

    题目链接 参考链接 只附代码好了 #include<bits/stdc++.h> using namespace std; ; int a[N],b[N],vis[N]; int n,m, ...

  4. [CSP-S模拟测试]:字符消除2(hash+KMP)

    题目背景 生牛哥终于打通了“字符消除”,可是他又被它的续集难倒了. 题目传送门(内部题52) 输入格式 第一行$n$表示数据组书.接下来每行一个字符串.(只包含大写字母) 输出格式 每组数据输出一个$ ...

  5. php 统计每天价格,货币种类,汇总得算法和数据处理 (后端和前段实现自动统计价格和币种类型)

    整体实现逻辑介绍 1.对查询数据做一个总体查询,需要根据查询自己主要业务逻辑数据. 2.对总体查询数据和时间和币种类型做三部分数据处理. 3.总体查询数据按照币种和日期组装二维数据对应得键值是价格. ...

  6. docker镜像加速遇见的一个问题

    今天运行docker发现了一个问题,运行docker images会报 Cannot connect to the Docker daemon at unix:///var/run/docker.so ...

  7. /proc/interrupts /proc/stat 查看中断信息

    /proc/interrupts列出当前所以系统注册的中断,记录中断号,中断发生次数,中断设备名称 如下图:从左至右:中断号   中断次数  中断设备名称 从上图可知中断号为19的arch_timer ...

  8. JS 提取公式中的参数

    'A+B-C/D*E'.split(/[*/()+-]/)  => [A,B,C,D,E]

  9. 美化Windows

    更改壁纸 https://www.omgubuntu.co.uk/2010/09/a-look-back-at-every-ubuntu-default-wallpaper google: ubunt ...

  10. 干货 | Bokeh交互式数据可视化快速入门

    Bokeh简介 Bokeh是一款交互式可视化库,在浏览器上进行展示. Bokeh可以通过Python(或其它语言),快速便捷地为大型流数据集提供优雅简洁的高性能交互式图表. 安装 在python中有多 ...