【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.

想法一:

用01数组代表该数字是否存在序列中,化归为:一个01序列中,最长连续1的问题。

小数据上可以,但是大数据测试上遇到了问题,当数据跨度很大,比如INT_MIN~INT_MAX时,空间复杂度太高。

想法二:

用map代替数组实现上面的想法,可以解决空间复杂度问题。

但是在计算最长连续1的过程中发现新的问题,遍历INT_MIN~INT_MAX的时间复杂度太高。

参考了Discussion中的一些讨论,

想法三:

对1的元素进行左右最大扩展计数,访问过的元素置0防止计数冗余,代替全范围的遍历计数。


class Solution {
public:
int longestConsecutive(vector<int> &num)
{
map<int, bool> tag;
for(vector<int>::size_type i = 0; i < num.size(); i ++)
{// true 代表未访问过
tag[num[i]] = true;
}

int count = 0;

for(vector<int>::size_type i = 0; i < num.size(); i ++)
{
int curcount = 0;

if(tag.find(num[i]) != tag.end() && tag[num[i]] == true)
{
tag[num[i]] = false;

curcount ++;

int left = num[i]-1;
while(tag.find(left) != tag.end() && tag[left] == true)
{
tag[left] = false;
curcount ++;
left -= 1;
}

int right = num[i]+1;
while(tag.find(right) != tag.end() && tag[right] == true)
{
tag[right] = false;
curcount ++;
right += 1;
}

if(curcount > count)
count = curcount;
}
}
return count;
}
};

【LeetCode】Longest Consecutive Sequence,布布扣,bubuko.com

时间: 2024-10-25 12:44:43

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

【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

【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 OJ - Longest Consecutive Sequence

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

【leetcode】 Longest Substring Without Repeating Characters

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

[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

【哈希表】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][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.