题目来源:

  https://leetcode.com/problems/longest-consecutive-sequence/


题意分析:

  给定一个没有排好序的数组,找到最长的连续序列的长度。要求时间复杂度是O(n)。比如[100, 4, 200, 1, 3, 2],其最长长度是[1,2,3,4]长度为4.


题目思路:

  对于每个数记录他所在的目前最长的序列,将其±1,如果其也在给定序列中,那么更新他所在的最长序列,比如上面的例子。所对应的序列变化是:

1. 100:[100,100];

2.100:[100,100];4:[4,4];

3.100:[100,100];4:[4,4];200:[200,200];

4.100:[100,100];4:[4,4];200:[200,200];1:[1,1];

5. 100:[100,100];4:[3,4];200:[200,200];1:[1,1];3 :[3,4];

6.100:[100,100];4:[1,4];200:[200,200];1:[1,4];3 :[3,4],2:[1,2];

所以得到答案是4


代码(python):

  

 class Solution(object):
def longestConsecutive(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
count = {}
ans = 0
if len(nums) != 0: ans = 1
for i in nums:
#ans = max(ans,1)
if i in count: continue
count[i] = [i,i]
if i - 1 in count:
#print(i - 1,count[i - 1])
count[count[i - 1][0]][1],count[i][0] = count[i][1],count[i - 1][0]
#print(count[i - 1][0],count[count[i - 1][0]],i,count[i])
ans = max(ans,count[count[i - 1][0]][1] + 1 - count[count[i - 1][0]][0])
if i + 1 in count:
#print(i + 1,count[i + 1])
count[count[i + 1][1]][0],count[count[i][0]][1] = count[i][0],count[i+1][1]
ans = max(ans,count[count[i + 1][1]][1] - count[count[i + 1][1]][0] + 1)
#print(count[i + 1][1],count[count[i + 1][1]],count[i][0],count[count[i][0]])
#for i in count:
#print(i,count[i])
#ans = max(ans,count[i][1] - count[i][0] + 1)
return ans

[LeetCode]题解(python):128-Longest Consecutive Sequence的更多相关文章

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

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

  2. [LeetCode] 298. Binary Tree Longest Consecutive Sequence 二叉树最长连续序列

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

  3. [LeetCode] 549. Binary Tree Longest Consecutive Sequence II 二叉树最长连续序列之 II

    Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree. Especia ...

  4. [LeetCode] 128. Longest Consecutive Sequence 解题思路

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

  5. 128. Longest Consecutive Sequence(leetcode)

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

  6. 【LeetCode】128. Longest Consecutive Sequence

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

  7. LeetCode 549. Binary Tree Longest Consecutive Sequence II

    原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence-ii/description/ 题目: G ...

  8. LeetCode 298. Binary Tree Longest Consecutive Sequence

    原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/ 题目: Given a binary t ...

  9. Java for LeetCode 128 Longest Consecutive Sequence

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

  10. leetcode 128. Longest Consecutive Sequence ----- java

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

随机推荐

  1. 怎样在一个页面使多个setInterval函数正常执行

    var firstInterval; var secondInterval; function firstAlert(){ if(firstInterval) clearInterval(firstI ...

  2. 从零单排PAT1015,1016,1017,1018

    1015德才论 题目要求: 输入格式: 输入第1行给出3个正整数,分别为:N(<=105),即考生总数.L(>=60).为录取最低分数线,即德分和才分均不低于L的考生才有资格被考虑录取:H ...

  3. nyist 303序号互换(数学推理)

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=303 思路: 开始看错题了,以为最多只有两个字母. 字母转数字的表达式很容易看出来是:(2 ...

  4. SMTP协议分析

    SMTP协议分析 第1章.     SMTP概述 1.1.  SMTP在邮件通信中的位置 SMTP,即简单邮件传送协议,所相应RFC文档为RFC821.同http等多数应用层协议一样,它工作在C/S模 ...

  5. mywebsite1

    http://www.wuweierwei.com/ 个人网站 http://www.yyyweb.com/demo/icons-filling/index.html CSS3图标填充效果 http: ...

  6. java 对象数组定义

    下面代码实现了定义一个数组对象 public class Student { private String username; private int num; public Student(Stri ...

  7. 微信支付 v 3.3.6

    文字说明: 前提:注册.申请服务号,开通微信支付. 涉及到的参数:AppId.AppSecret.原始ID(自动回复).mch_id(商户号).Key(商户密钥:自己设定.) 统一规范: 要求 认证方 ...

  8. 根据不同需求跳转不同Activity的另外一种写法

    代码如下: /* Android Asynchronous Http Client Sample Copyright (c) 2014 Marek Sebera <marek.sebera@gm ...

  9. [转载]Heritrix 提高效率的若干方法

    摘自http://blog.sina.com.cn/s/blog_6cc084c90100nf39.html --------------------------------------------- ...

  10. VM Agent 和扩展程序

    VM Agent 和扩展程序 - 第 1 部分  Windows Azure基础结构服务最近宣布了一项新功能VM Agent.VMAgent是一个轻量级进程,用于启动由Microsoft或合作伙伴 ...