原题地址: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. 表达式括号匹配(stack.cpp)

    [问题描述]        假设一个表达式有英文字母(小写).运算符(+,—,*,/)和左右小(圆)括号构成,以“@”作为表达式的结束符.请编写一个程序检查表达式中的左右圆括号是否匹配,若匹配,则返回 ...

  2. [USACO08OCT]Watering Hole

    [USACO08OCT]Watering Hole 题目大意: Farmer John 有\(n(n\le300)\)个牧场,他希望灌溉他的所有牧场.牧场编号为\(1\sim n\),要灌溉一个牧场有 ...

  3. 【转】NHibernate对象以及状态说明

    对象 ISessionFactory (NHibernate.ISessionFactory) 针对单个数据库映射关系经过编译后的内存镜像,是线程安全的(不可变). 它是生成ISession的工厂,本 ...

  4. 一些收集的MikroTik RouterOS破解版虚拟机VMware

    会不定期更新,也许后续自己来做,现在是收集.持续关注这个分享链接即可. 链接:https://pan.baidu.com/s/1j7ciesPm1yAgCJ26dLJ8sg  密码:i64w

  5. Java 与 .NET 的平台发展之争

    Java 8即将正式发布,从早期版本中,我们已经可以领略到一些令人兴奋的特性.但是开发者Andrew C. Oliver表示,尽管如此,Java语言在某些特性上还是落后于.Net.比如,Java 8中 ...

  6. Programming Internal Flash Over the Serial Wire Debug <SWD> Interface -- EFM32

    1 Debug Interface Overview 1.1 Serial Wire Debug Serial Wire Debug (SWD) is a two-wire protocol for ...

  7. Ubuntu 中启用 root 帐号

    参考:http://linuxtoy.org/archives/howto_enable_ubuntu_root_account.html 如果你实在需要在 Ubuntu 中启用 root 帐号的话, ...

  8. fritshoogland 大神ORACLE :pga-memory-operation latch

    https://fritshoogland.wordpress.com/2017/03/01/oracle-12-2-wait-event-pga-memory-operation/#comment- ...

  9. Python index()方法

    Python index()方法  Python 字符串 描述 Python index() 方法检测字符串中是否包含子字符串 str ,如果指定 beg(开始) 和 end(结束) 范围,则检查是否 ...

  10. 【pycharm】pycharm上安装tensorflow,报错:AttributeError: module 'pip' has no attribute 'main' 解决方法

    pycharm上安装tensorflow,报错:AttributeError: module 'pip' has no attribute 'main' 解决方法 解决方法: 在pycharm的安装目 ...