【leetcode】Longest Consecutive Sequence(hard)☆

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.

思路:先把大神的方法亮出来

use a hash map to store boundary information of consecutive sequence for each element; there 4 cases when a new element i reached:

1) neither i+1 nor i-1 has been seen: m[i]=1;

2) both i+1 and i-1 have been seen: extend m[i+m[i+1]] and m[i-m[i-1]] to each other;

3) only i+1 has been seen: extend m[i+m[i+1]] and m[i] to each other;

4) only i-1 has been seen: extend m[i-m[i-1]] and m[i] to each other.

int longestConsecutive(vector<int> &num) {
    unordered_map<int, int> m;
    int r = 0;
    for (int i : num) {
        if (m[i]) continue; //跳过重复
        r = max(r, m[i] = m[i + m[i + 1]] = m[i - m[i - 1]] = m[i + 1] + m[i - 1] + 1); //如果新的数字把左右连起来了,则把该连起来的序列的第一个数m[i - m[i - 1]]和最后一个数字m[i + m[i + 1]] 设为新的长度!
    }
    return r;
}

下面是我自己写的,可谓血与泪的历史。

首先O(n)表示一定不能排序, 那如何获得左右邻接的信息呢。 肯定要用hash了。结果花了2个小时,写了一个超复杂的代码。虽然AC,但是.......唉...................................................

int longestConsecutive(vector<int> &num) {
        //去重复
        unordered_set<int> s;
        for(int i = 0; i < num.size(); i++)
        {
            if(s.find(num[i]) == s.end())
                s.insert(num[i]);
            else
                num.erase(num.begin() + (i--));
        }

        int maxlen = 0;
        unordered_map<int, int> first; //记录每个连续序列的第一个数字在record中的哪个位置
        unordered_map<int, int> last; //记录每个连续序列的最后一个数字在record中的哪个位置
        vector<vector<int>> record; //记录每个连续序列的第一个数字和最后一个数字是什么
        for(int i = 0; i < num.size(); i++)
        {
            int pre, post;
            unordered_map<int, int>::iterator f = first.find(num[i] + 1);
            unordered_map<int, int>::iterator l = last.find(num[i] - 1);
            if(f != first.end() && l != last.end())
            {
                pre = l->second;
                post = f->second;
                //修改hash表
                last.erase(record[pre][1]);
                first.erase(record[post][0]);
                last[record[post][1]] = pre;
                //修改记录的区间
                record[pre][1] = record[post][1];
                maxlen = (maxlen > record[pre][1] - record[pre][0] + 1) ? maxlen :  record[pre][1] - record[pre][0] + 1;
                record[post].clear();
            }
            else if(f != first.end())
            {
                post = f->second;
                //修改hash
                first.erase(record[post][0]);
                first[num[i]] = post;
                //修改区间
                record[post][0] = num[i];
                maxlen = (maxlen > record[post][1] - record[post][0] + 1) ? maxlen :  record[post][1] - record[post][0] + 1;
            }
            else if(l != last.end())
            {
                pre = l->second;
                last.erase(record[pre][0]);
                last[num[i]] = pre;

                record[pre][1] = num[i];
                maxlen = (maxlen > record[pre][1] - record[pre][0] + 1) ? maxlen :  record[pre][1] - record[pre][0] + 1;
            }
            else
            {
                record.push_back(vector<int>(2, num[i]));
                first[num[i]] = record.size() - 1;
                last[num[i]] = record.size() - 1;
                maxlen = (maxlen > 1) ? maxlen : 1;
            }
        }
        return maxlen;
    }
时间: 2024-10-28 20:32:37

【leetcode】Longest Consecutive Sequence(hard)☆的相关文章

【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

leetcode 之Longest Consecutive Sequence(六)

这题要仔细体会下哈希表的用法,要注意的是数组本身是无序的,因此需要向左右进行扩张. 另外这个思路可以进行聚类,把连续的标记为一类. int longestConsecutive(const vector<int> &num) { unordered_map<int, bool> used; for (auto i : num)used[i] = false; int longest = 0; for (auto i : num) { if (used[i])continue

【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

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 b

[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 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】 Longest Substring Without Repeating Characters

题目: 给定一个字符串,返回该串没有重复字符的最长子串. 分析: 1)子串:子串要求是连续的. 2)无重复,出现重复就断了,必须从新的位置开始.而新的位置就是重复字符第一次出现位置的下一个位置. 3)整个串可能没有一处重复. 那么,为了找出当前访问的字符是否出现过,要怎么做呢?当然是hash,O(1)的时间,而且既然是字符, 定义个255的hash table 就可以了,hash table 中的元素为相应字符在s中出现的位置.初始化为-1,表示都没有出现. 我们另外定义一个start 和end