Problem Link:

http://oj.leetcode.com/problems/longest-consecutive-sequence/

This problem is a classical problem where we can reduce the running time by the help of hash table.

By given a list of numbers, we can find the longest consecutive sequence by the following steps:

  1. Let H be a empty hash set, add all given numbers into H (duplicates will be removed)
  2. Let max_len = 0 denote the length of current longest consecutive sequence
  3. While H is not empty:
    1. count all n's smaller consecutive numbers in H and remove them from H
    2. count all n's larger consecutive numbers in H and remove them from H
    3. update max_len with the length of this consecutive sequence containing n

The python code is as follows.

class Solution:
# @param num, a list of integer
# @return an integer
def longestConsecutive(self, num):
"""
Find the longest consecutive number sequence by using hash map
1) Add all numbers in the list to a hash set HS
2) Let max_length = 0, which records the length of the current longest consecutive sequence
3) For each number n in the hash set
count the number of all n's left consecutive numbers in the hash set
count the number of all n's right consecutive numbers in the hash set
remove the counted numbers from the hash set
Update the max_length with the length of this consecutive sequence contaning n.
"""
# Conver the list to a hash set, this will remove the duplicates
numbers = set(num)
# Current max_len
max_len = 0 while numbers:
# Get a number from the hash set
x = numbers.pop()
# This sequence only containing x is length of 1
x_len = 1
# Find all left consecutive numbers of x
left = x-1
while left in numbers:
numbers.remove(left)
left -= 1
# Find all right consecutive numbers of x
right = x+1
while right in numbers:
numbers.remove(right)
right += 1
# Update the max_len
max_len = max(max_len, right-left-1) return max_len

【LeetCode OJ】Longest Consecutive Sequence的更多相关文章

  1. 【LeetCode OJ】Longest Palindromic Substring

    题目链接:https://leetcode.com/problems/longest-palindromic-substring/ 题目:Given a string S, find the long ...

  2. 【LeetCode OJ】Longest Substring Without Repeating Characters

    题目链接:https://leetcode.com/problems/longest-substring-without-repeating-characters/ 题目:Given a string ...

  3. 【leetcode】Longest Consecutive Sequence

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

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

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

  5. LeetCode Binary Tree Longest Consecutive Sequence

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

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

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

  7. 【leetcode】Longest Consecutive Sequence(hard)☆

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

  8. 【leetcode刷题笔记】Longest Consecutive Sequence

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

  9. 【LeetCode OJ】Interleaving String

    Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...

随机推荐

  1. Qt之事件系统

    简述 在Qt中,事件就是对象,派生自QEvent抽象类,用来表示在应用程序中发生的事件,或是应用程序需要处理的外部活动产生的事件. Events可以被任何QObject派生的子类实例对象接收和处理,但 ...

  2. hdu----(5045)Contest(数位dp)

    Contest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

  3. java设计之简单的JAVA计算器

    做这个东西主要是为了练习一下以前学习过的java Swing,所以那些复杂的算法就没有加载到里面去........ 先展示一下效果....,额,当然不是这个样子....   ---> 代码: p ...

  4. 张艾迪(创始人):DCM的不识人.我说我会像乔布斯一样成为投资者的骄傲

      Eidyzhang解码:天才Eidyzhang的诞生 张艾迪(创始人):第一个实习生精英团队 张艾迪(创始人):DCM的不识人.我说我会像乔布斯一样成为投资者的骄傲 2014-05-31 09:4 ...

  5. #define && const

    (1) 编译器处理方式不同 define宏是在预处理阶段展开. const常量是编译运行阶段使用. (2) 类型和安全检查不同 define宏没有类型,不做任何类型检查,仅仅是展开. const常量有 ...

  6. uva 10118

    10118 - Free Candies Time limit: 30.000 seconds Little Bob is playing a game. He wants to win some c ...

  7. MySql查找几个字段的值一样的记录

    ),name,class,charge_start,charge_end ) ; 注意:having在这里起到很大的作用,只有在having中,才可以对类似sum(),count()等等复合函数的结果 ...

  8. POJ 3206 最小生成树

    DESCRIPTION:T_T 在下是读不懂题意的.但是捏.现在知道是求把所有的点(是字母的点)连起来的最小的权值.即最小生成树.因为求最小生成树是不计较源点是哪个的.所以可以把A和S看成一样的.首先 ...

  9. ASP.NET MVC统一异常处理

    前言: 今早看了篇文章:求知成瘾,却无作品 的思考:很有感触,发现原来自己也是这样,对每样东西都抱有很大的兴趣或者希望自己去学,一年后发现原来自己什么都是皮毛什么都不精!最终发现真正的大牛都是在某一个 ...

  10. java面向对象编程——第二章 java基础语法

    第二章 java基础语法 1. java关键字 abstract boolean break byte case catch char class const continue default do ...