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. MySQL学习笔记_2_MySQL创建数据表(上)

    MySQL创建数据表(上) 一.创建数据表的SQL语句模型[弱类型] CREATETABLE [IF NOT EXISTS] 表名称( 字段名1列的类型[属性][索引], 字段名2 列的类型[属性][ ...

  2. RAID5和RAID10,哪种RAID更适合你(上)

    [IT168 专稿]存储是目前IT产业发展的一大热点,而RAID技术是构造高性能.海量存储的基础技术,也是构建网络存储的基础技术.专家认为,磁盘阵列的性能优势得益于磁盘运行的并行性,提高设备运行并行度 ...

  3. 如何在win7下配置IIS?

  4. 使用strace工具故障排查的5种简单方法

    使用strace工具故障排查的5种简单方法 本文源自5 simple ways to troubleshoot using strace strace 是一个非常简单的工具,用来跟踪可执行程序的系统调 ...

  5. JSP 客户端请求

    当浏览器请求一个网页时,它会向网络服务器发送一系列不能被直接读取的信息,因为这些信息是作为HTTP信息头的一部分来传送的.您可以查阅HTTP协议来获得更多的信息. 下表列出了浏览器端信息头的一些重要内 ...

  6. 在Ios里UIWebView参入js

    //修改图片大小适应webView宽高度            [webView stringByEvaluatingJavaScriptFromString:       @"var sc ...

  7. tableView里删除单元格

    tableView里删除单元格 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSInde ...

  8. (转载)全球唯一标识GUID

    GUID(Global unique identifier)全局唯一标识符,它是由网卡上的标识数字(每个网卡都有唯一的标识号)以及 CPU 时钟的唯一数字生成的的一个 16 字节的二进制值. GUID ...

  9. 微软WTL模板库完整版安装(VS2010+windows7X64位环境下)分享

    一:简介 想必大家对于微软的MFC应该都比较熟悉.但是WTL可能很多人比较陌生吧.下面我就简单的说说这个库. 首先对这个库的做个简单的介绍吧. WTL 是 Windows Template Libra ...

  10. PHPExcel 学习笔记

    首先到phpexcel官网上下载最新的phpexcel类,下周解压缩一个classes文件夹,里面包含了PHPExcel.php和PHPExcel的文件夹,这个类文件和文件夹是我们需要的,把class ...