题目:

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),很自然的想到哈希表,只有哈希表的查找时间为O(1)。

实现代码:

#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std; /*
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.
*/
class Solution {
public:
int longestConsecutive(vector<int> &num) {
if(num.empty())
return 0;
unordered_set<int> iset;
vector<int>::iterator iter;
for(iter = num.begin(); iter != num.end(); ++iter)
iset.insert(*iter); int max = 0;
for(iter = num.begin(); iter != num.end(); ++iter)
{
if(iset.count(*iter) == 1)
{
int c = 1;
int g = *iter + 1;
while(iset.count(g) == 1)
{
iset.erase(g);//找到后记得要删除,不然会重复做,超时
c++;
g++;
}
int l = *iter - 1;
while(iset.count(l) == 1)
{
iset.erase(l);
c++;
l--;
}
if(max < c)
max = c; } }
return max;
}
};
int main(void)
{
int arr[] = {100, 4, 200, 1, 3, 2};
int n = sizeof(arr) / sizeof(arr[0]);
vector<int> num(arr, arr+n);
Solution solution;
int max = solution.longestConsecutive(num);
cout<<max<<endl;
return 0;
}

LeetCode128:Longest Consecutive Sequence的更多相关文章

  1. leetcode解题报告(5):Longest Consecutive Sequence

    描述 Given an unsorted array of integers, find the length of the longest consecutive elements sequence ...

  2. 【LeetCode-128】Longest Consecutive Sequence

    描述 输入一个乱序的连续数列,输出其中最长连续数列长度,要求算法复杂度为 O(n) . 输入 54,55,300,12,56 输出 3 通常我们看有没有连续序列时 找某个数有没有的前后的数,比如看到5 ...

  3. [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

  4. [LeetCode] Longest Consecutive Sequence 求最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  5. Binary Tree Longest Consecutive Sequence

    Given a binary tree, find the length of the longest consecutive sequence path (连续的路径,不是从小到大). The pa ...

  6. [LintCode] Longest Consecutive Sequence 求最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. H ...

  7. LeetCode Binary Tree Longest Consecutive Sequence

    原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/ 题目: Given a binary t ...

  8. 24. Longest Consecutive Sequence

    Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...

  9. 【leetcode】Longest Consecutive Sequence

    Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...

随机推荐

  1. 【网络编程】——linux socket demo

    #include <stdio.h> #include <string.h> #include <stdlib.h> #include <sys/socket ...

  2. Text3d

    有小bug,先弄这么多吧,晚了,碎觉了 ---------------------------------

  3. java之Object类介绍

    1.Object类是所有java类的基类 如果在类的声明中未使用extends关键字指明其基类,则默认基类为Object类,ex: public class Person{ ~~~~~ } 等价于 p ...

  4. using-ef-code-first-with-an-existing-database

    http://weblogs.asp.net/scottgu/using-ef-code-first-with-an-existing-database http://weblogs.asp.net/ ...

  5. sap IRfcTable 转成 DataTable

    public DataTable GetDataTableFromRFCTable(IRfcTable myrfcTable) { DataTable loTable = new DataTable( ...

  6. eclipse 手动/自动安装插件

    只要你的Eclipse的压缩包,一般为xxx.zip,其内部包含了对应的features和plugins文件夹,(不管是否还有content.jar和artifacts.jar)则都可以: 要么手动解 ...

  7. C#基础总结之四List-Hashtable-冒泡排序

    #region 第四天作业 名片--------ArrayList //ArrayList card = new ArrayList(); //card.Add("2202111001122 ...

  8. ruby -- 问题解决(二)rails4.0create引起的ActiveModel::ForbiddenAttributesError错误

    之前将rails升级到4.0版本,发生了ActiveModel::ForbiddenAttributesError错误 于是上网溜达了一会,找到解决方案, ActiveModel::Forbidden ...

  9. ServiceStack Redis客户端的bug

    client.Set("key", 0); 当使用上面的语句设置 真正存入redis的却是一个空白字符,而不是0 跟了一下源码,发现 private static byte[] T ...

  10. 怎样设置一个DIV在所有层的最上层,最上层DIV

    怎样设置一个DIV在所有层的最上层,最上层DIV,其实很简单,只需要在这个DIV上使用这个样式即可,z-index:99999