【LeetCode OJ】Longest Consecutive Sequence

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.

?





1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

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,布布扣,bubuko.com

时间: 2024-10-05 06:16:11

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

【LeetCode OJ】Longest Palindromic Substring

题目链接:https://leetcode.com/problems/longest-palindromic-substring/ 题目:Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. 解题思路:p

【leetcode刷题笔记】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

【LeetCode】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

【LeetCode OJ】Sum Root to Leaf Numbers

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 # Definition for a  binary tree node # class TreeNode: #     def __init__(self, x): #         self.val = x #         self.left = No

【LeetCode OJ】Word Ladder I

Problem Link: http://oj.leetcode.com/problems/word-ladder/ Two typical techniques are inspected in this problem: Hash Table. One hash set is the words dictionary where we can check if a word is in the dictionary in O(1) time. The other hash set is us

【哈希表】Longest Consecutive Sequence

题目:leetcode 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 i

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

Given a binary tree, find the length of the longest consecutive sequence path. The path refers to any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The longest consecutive path need to be from p

Leetcode: Binary Tree Longest Consecutive Sequence

Given a binary tree, find the length of the longest consecutive sequence path. The path refers to any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The longest consecutive path need to be from p

【LeetCode】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