[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 O(n) complexity.

这道题要求求最长连续序列,并给定了O(n)复杂度限制,所以很自然想到了哈希表,其查找速度为常数级,我们的解题思路是先把原数组中的所有数存入哈希表中,然后对于每一个数字,在其递增递减方向依次搜索,并记录连续长度,每遍历完一个数字,便将其移出哈希表,最终遍历完所有数字,可得答案。代码如下:

class Solution {
public:
    int longestConsecutive(vector<int> &num) {
        int res = 0;
        unordered_map<int, int> numMap;
        for (int i = 0; i < num.size(); ++i) {
            numMap[num[i]] = i;
        }
        for (int i = 0; i < num.size(); ++i) {
            int d = num[i];
            int n = 1;
            numMap.erase(d);
            while (numMap.find(++d) != numMap.end()) {
                ++n;
                numMap.erase(d);
            }
            d = num[i];
            while (numMap.find(--d) != numMap.end()) {
                ++n;
                numMap.erase(d);
            }
            res = max(res, n);
        }
        return res;
    }
};
时间: 2024-10-01 15:48:24

[LeetCode] Longest Consecutive Sequence 求最长连续序列的相关文章

[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(DP)

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 i

[leetcode]Longest Consecutive Sequence @ Python

原题地址: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 seque

LeetCode: Longest Consecutive Sequence 解题报告

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.

[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 ru

[Leetcode] Longest Consecutive Sequence 略详细 (Java)

题目参见这里 https://leetcode.com/problems/longest-consecutive-sequence/ 这个题目我感觉很难,看了半天别人写的答案,才明白个所以然.下面的代码是我自己的改编,写的好像很复杂的样子,主要也是为了方便自己理解,耐着性子看完,应该就理解了. [个人分析]: 题目难度主要是要求O(N)完成.本来最自然的想法是先排序然后再去扫一遍得到结果,可是这样,明显就是O(NlgN).怎么样从NlgN 提高到O(N)呢?  ==> 空间换时间,空间从O(1)

[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 O(n

LeetCode: Longest Consecutive Sequence [128]

[题目] 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