LeetCode[Map]: 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.

参考:https://oj.leetcode.com/discuss/18886/my-really-simple-java-o-n-solution-accepted

思路如下:用一个map存储所有连续序列的长度,并保存在以序列头尾元素对应的value中。举例来说,对于序列{1,2,3,4,5},map[1]和map[5]应该都等于5。

过程:遍历整个输入数组,对于每一个新元素n:

1. 当前元素如果已经存在在map中,那么直接跳过该元素。

2. 如果n-1和n+1存在在map中,那么说明邻近n有已经存在的序列。变量left和right分别存储两边序列的长度,而0则表示没有已经存在的序列,那么n将是序列的边界。

3. 利用left和right来定位左侧和右侧边界,更新边界和当前n的value均为sum = left + 1 + right。

C++代码实现如下:

    int longestConsecutive(vector<int> &num) {
        unordered_map<int, int> myMap;
        int res = 0;
        for (auto n : num) {
            if (myMap.find(n) == myMap.end()) {
                int left  = myMap.find(n - 1) == myMap.end() ? 0 : myMap[n - 1];
                int right = myMap.find(n + 1) == myMap.end() ? 0 : myMap[n + 1];

                int sum = left + 1 + right;
                if (sum > res) res = sum;
                myMap[n] = myMap[n - left ] = myMap[n + right] = sum;
            }
        }

        return res;
    }

时间性能表现如下:

时间: 2024-10-09 23:37:53

LeetCode[Map]: 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 its length: 4. Your algorithm should run in

LeetCode OJ - Longest Consecutive Sequence

这道题中要求时间复杂度为O(n),首先我们可以知道的是,如果先对数组排序再计算其最长连续序列的时间复杂度是O(nlogn),所以不能用排序的方法.我一开始想是不是应该用动态规划来解,发现其并不符合动态规划的特征.最后采用类似于LRU_Cache中出现的数据结构(集快速查询和顺序遍历两大优点于一身)来解决问题.具体来说其数据结构是HashMap<Integer,LNode>,key是数组中的元素,所有连续的元素可以通过LNode的next指针相连起来. 总体思路是,顺序遍历输入的数组元素,对每个

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

[LeetCode][JavaScript]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.

[Lintcode]124. Longest Consecutive Sequence/[Leetcode]128. Longest Consecutive Sequence

124. Longest Consecutive Sequence/128. Longest Consecutive Sequence 本题难度: Medium/Hard Topic: Data Structure Description Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Example Given [100, 4, 200, 1,

Leetcode 贪心 Longest Consecutive Sequence

Longest Consecutive Sequence Total Accepted: 19169 Total Submissions: 68303My Submissions 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 consec

【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

Java for LeetCode 128 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

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