【LeetCode OJ】Longest Consecutive Sequence

时间:2022-09-01 21:13:13

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. o(1)复杂度之双边滤波算法的原理、流程、实现及效果。

    一.引言     双边滤波在图像处理领域中有着广泛的应用,比如去噪.去马赛克.光流估计等等,最近,比较流行的Non-Local算法也可以看成是双边滤波的一种扩展.自从Tomasi et al等人提出该 ...

  2. http状态码详解

    1 网址协议不支持的协议. 2 检测器内部错误. 3 网址格式不正确. 5 无法连接到代理服务器. 6 无法连接到服务器或找不到域名. 7 连接服务器失败. 28 操作超时.可能原因:页面执行时间过长 ...

  3. 通过js动态生成页面表格

    var redlineTemplateP = $(".redlineDataList"); for (var index in detailArraryLists.rows){ v ...

  4. python : HTML+CSS (定时器轮寻)

    定时器轮寻 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  5. 怎么获取iOS的私有API

    前言 作为iOS开发人员,相信大多数伙伴都对怎么获取iOS的私有API很有兴趣,最近通过查找资料,总结了以下三种方法,希望对你有用. 第一种(class-dump) 不得不说这是一个很棒的工具,安装和 ...

  6. 怎么在阿里云服务器部署多个tomcat

    部署前准备: 1.到阿里云官网购买一台服务器 2.给阿里云服务器挂盘,阿里云有教程这里不讲解,自己看. Linux 系统挂载数据盘 视频:Linux服务器挂载数据盘 3.下载tomcat  http: ...

  7. &period;Net高级技术

    本次课程中讲的有的东西都是根据初学者的认知规律进行了调整,并不是严谨的,比如很多地方在多AppDomain条件下很多说法就不对了,但是说严谨了大家就晕了,因此继续不严谨的讲吧. 很多面试题都在这阶段的 ...

  8. C&plus;&plus; trivial和non-trivial构造函数及POD类型(转)

    原博客地址http://blog.csdn.net/a627088424/article/details/48595525 最近正纠结这个问题就转过来了,做了点补充(参考<深度探索C++对象模型 ...

  9. 转:Twitter&period;com在用哪些Javascript框架?

    原文来自于:http://blog.jobbole.com/63964/ 我一直在研究twitter.com使用的一些UI框架.下面是这些框架的清单(大部分是Javascript框架).如果你发现有些 ...

  10. HTML5实际和离线应用分析

    当前离线Web申请书,即,该装置不能访问因特网时的应用的执行.HTML5离线应用重点,主要开发人员希望.步骤离线应用开发有:首先我们应该知道设备是否可以连接;然后,它也应该可以访问某些资源(像.CSS ...