原题地址:https://oj.leetcode.com/problems/longest-consecutive-sequence/

题意:

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

For example,
Given [100, 4, 200, 1, 3, 2],
The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4.

Your algorithm should run in O(n) complexity.

解题思路:使用一个哈希表,在Python中是字典dict数据类型。dict中的映射关系是{x in num:False},这个表示num中的x元素没有被访问过,如果被访问过,则为True。如果x没有被访问过,检查x+1,x+2...,x-1,x-2是否在dict中,如果在dict中,就可以计数。最后可以求得最大长度。

代码:

class Solution:
# @param num, a list of integer
# @return an integer
def longestConsecutive(self, num):
dict = {x: False for x in num} # False means not visited
maxLen = -
for i in dict:
if dict[i] == False:
curr = i+; lenright =
while curr in dict:
lenright += ; dict[curr] = True; curr +=
curr = i-; lenleft =
while curr in dict:
lenleft += ; dict[curr] = True; curr -=
maxLen = max(maxLen, lenleft++lenright)
return maxLen

[leetcode]Longest Consecutive Sequence @ Python的更多相关文章

  1. LeetCode——Longest Consecutive Sequence

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

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

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

  3. LeetCode: Longest Consecutive Sequence 解题报告

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

  4. [LeetCode] Longest Consecutive Sequence

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

  5. LeetCode: Longest Consecutive Sequence [128]

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

  6. Leetcode: Longest Consecutive Sequence && Summary: Iterator用法以及ConcurrentModificationException错误说明

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

  7. LeetCode—Longest Consecutive Sequence

    题目描述: Given an unsorted array of integers, find the length of the longest consecutive elements seque ...

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

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

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

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

随机推荐

  1. BZOJ.4032.[HEOI2015]最短不公共子串(DP 后缀自动机)

    题目链接 1.求A的最短子串,它不是B的子串. 子串是连续的,对B建SAM,枚举起点,在SAM上找到第一个无法匹配点即可.O(n)用SAM能做吗..开始想错了. 2.求A的最短子串,它不是B的子序列. ...

  2. BZOJ.1076.[SCOI2008]奖励关(概率DP 倒推)

    题目链接 BZOJ 洛谷 真的题意不明啊.. \(Description\) 你有k次选择的机会,每次将从n种物品中随机一件给你,你可以选择选或不选.选择它会获得这种物品的价值:选择一件物品前需要先选 ...

  3. AIM Tech Round 3 (Div. 1) A. Letters Cyclic Shift 贪心

    A. Letters Cyclic Shift 题目连接: http://www.codeforces.com/contest/708/problem/A Description You are gi ...

  4. Nginx配置直接php

    nginx配置文件: server { listen 80; server_name localhost; access_log /var/log/nginx/log/host.access.log ...

  5. AE开发中关于 “无法嵌入互操作类型.........请改用适用的接口”问题的解决方法

    最近开始使用VS2010,在引用COM组件的时候,出现了“无法嵌入互操作类型……,请改用适用的接口”的错误提示. 查阅资料,找到解决方案,记录如下: 选中项目中引入的dll,鼠标右键,选择属性,把“嵌 ...

  6. How to Set Up DTrace to Detect PHP Scripting Problems on Oracle Linux

    http://www.oracle.com/technetwork/articles/servers-storage-admin/php-dtrace-linux-2062229.html

  7. [转]Twemproxy 介绍与使用

    Twemproxy是一种代理分片机制,由Twitter开源.Twemproxy作为代理,可接受来自多个程序的访问,按照路由规则,转发给后台的各个Redis服务器,再原路返回.该方案很好的解决了单个Re ...

  8. Windows Phone本地数据库(SQLCE):1、介绍(翻译)(转)

    一只大菜鸟,最近要学习windows phone数据库相关的知识,找到了一些比较简短的教程进行学习,由于是英文的,顺便给翻译了.本身英语水平就不好,估计文中有不少错误,如果有不幸读到的童鞋请保持对翻译 ...

  9. spring集成jpa【为什么有 persistant.xml 文件呢?】

    原文地址: http://www.cnblogs.com/javahuang/archive/2012/12/19/2824633.html spring集成JPA的其中一种方式 JPA和hibern ...

  10. arcgis导oracle多步操作产生错误。请检查每一步的状态值。" 如何解决?

    你知你用的什么数据引擎,ADO? 我以前碰过类似的,我有两个方案:   1.升ado到2.7以上      2.不要用microsoft oledb provider for oracle,而要用or ...