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

题解:

Solution 1

class Solution {
public:
    int longestConsecutive(vector<int>& nums) {int longest_len = 0;
        unordered_map<int, bool> map;
        for(auto n : nums)
            map[n] = false;
        for(auto i : nums){
            if(map[i])
                continue;
            int len = 1;
            for(int j = i + 1; map.find(j) != map.end(); ++j){
                map[j] = true;
                ++len;
            }
            for(int j = i - 1; map.find(j) != map.end(); --j){
                map[j] = true;
                ++len;
            }
            longest_len = max(longest_len, len);
        }
        return longest_len;
    }
};

Solution 2

class Solution {
public:
    int longestConsecutive(vector<int>& nums) {
        int longest_len = 0;
        unordered_set<int> set(nums.begin(), nums.end());
        for(auto i : nums){
            if(set.find(i) == set.end())
                continue;
            set.erase(i);
            int prev = i - 1, next = i + 1;
            while(set.find(prev) != set.end()) set.erase(prev--);
            while(set.find(next) != set.end()) set.erase(next++);
            longest_len = max(longest_len, next - prev - 1);
        }
        return longest_len;
    }
};

Solution 3

class Solution {
public:
    int longestConsecutive(vector<int>& nums) {
        int longest_len = 0;
        unordered_map<int, int> map;
        for(auto n : nums){
            if(map.find(n) != map.end())
                continue;
            int prevsum = map.find(n - 1) != map.end() ? map[n - 1] : 0;
            int nextsum = map.find(n + 1) != map.end() ? map[n + 1] : 0;
            int sum = prevsum + nextsum + 1;
            map[n] = sum;
            map[n - prevsum] = sum;
            map[n + nextsum] = sum;
            longest_len = max(sum, longest_len);
        }
        return longest_len;
    }
};
时间: 2024-10-13 12:33:25

【Leetcode】128. Longest Consecutive Sequence的相关文章

【leetcode】1027. Longest Arithmetic Sequence

题目如下: Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is

[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开心刷题五十六天——128. Longest Consecutive Sequence

最近刷题进展尚可,但是形式变化了下,因为感觉眼睛会看瞎,所以好多写在纸上.本来想放到文件夹存储起来,但是太容易丢了,明天整理下,赶紧拍上来把 今晚是周末,这一周都在不停的学学学,我想下周怕是不能睡午觉了,中午回去床对我的诱惑太大了,我得想办法,一进门先把被褥收起来,再放个欢快的歌,中午少吃点,加油小可爱 之前欠下的烂帐,把太多简单题做完,导致剩下的都是难题,所以万万记住一点捷径都不要走 128看花花酱大神的解法,发现对hashtable的了解十分不足,甚至一些常见函数都不知道是干什么的 这道题涉

128. Longest Consecutive Sequence(js)

128. Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Your algorithm should run in O(n) complexity. Example: Input: [100, 4, 200, 1, 3, 2] Output: 4 Explanation: The longe

[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

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] 128. Longest Consecutive Sequence

传送门 Description 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 algorith

128. Longest Consecutive Sequence(leetcode)

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