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.

Summary: The key idea is using hash map to record every number and their index.

     int longestConsecutive(vector<int> &num) {
unordered_map<int, int> num_idx;
for(int i = ; i < num.size(); i ++) {
num_idx[num[i]] = i;
} vector<bool> visited;
for(int i = ; i < num.size(); i ++) {
visited.push_back(false);
} int longest_len = ;
for(int i = ; i < num.size(); i ++) {
if(visited[i] == true)
continue; int tmp_len = ;
visited[i] = true;
//increase
int value = num[i] + ;
while(num_idx.find(value) != num_idx.end()){
int index = num_idx[value];
value ++;
visited[index] = true;
tmp_len ++;
}
//decrease
value = num[i] - ;
while(num_idx.find(value) != num_idx.end()){
int index = num_idx[value];
value --;
visited[index] = true;
tmp_len ++;
} if(tmp_len > longest_len){
longest_len = tmp_len;
if(longest_len > num.size() / )
break;
}
} return longest_len;
}

Longest Consecutive Sequence [LeetCode]的更多相关文章

  1. 128. Longest Consecutive Sequence(leetcode)

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

  2. Binary Tree Longest Consecutive Sequence -- LeetCode

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

  3. Longest Consecutive Sequence——Leetcode

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

  4. Longest Consecutive Sequence leetcode java

    题目: Given an unsorted array of integers, find the length of the longest consecutive elements sequenc ...

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

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

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

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

  7. LeetCode Binary Tree Longest Consecutive Sequence

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

  8. 【LeetCode OJ】Longest Consecutive Sequence

    Problem Link: http://oj.leetcode.com/problems/longest-consecutive-sequence/ This problem is a classi ...

  9. [LeetCode] 128. Longest Consecutive Sequence 解题思路

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

随机推荐

  1. VMware ESXI5.0的安装配置 zz

    http://www.hotxf.com/thread-297-1-1.html 1,    Vmware ESXI 光盘一张文件大小290M,本教程是以 5.0为案例. 2,    所需要安装的操作 ...

  2. 判断浏览器是否为IE内核的最简单的方法

    没啥说的,直接贴代码,算是ie hack了. if (!+[1,]) { alert('is ie'); }

  3. 当插入数据失败时,防止mysql自增长字段的自增长的方法

    问题描述: 当mysql设置了自增长字段时(注意:一个表中只能设置一个自增长字段,可以不是主键,但必须是键 ),如果插入数据失败,那么自增长字段仍然会占用这个自增长值,再次成功插入数据时就会造成断层. ...

  4. System.MissingMethodException: 找不到方法:

    This is a problem which can occur when there is an old version of a DLL still lingering somewhere ar ...

  5. 2016ACM/ICPC亚洲区大连站-重现赛

    题目链接:http://acm.hdu.edu.cn/search.php?field=problem&key=2016ACM%2FICPC%D1%C7%D6%DE%C7%F8%B4%F3%C ...

  6. 7.Constants and Fields

    1.Constants is a symbol that has a never-changing value.  its value must be determinable at compile ...

  7. 线程入门之join方法

    package com.thread; /** * <join:将某线程加入进来,相当于方法调用,也叫合并某个线程> * <功能详细描述> * * @author 95Yang ...

  8. wordpress中文标签无法访问的解决方法

    wordpress中文标签无法访问的解决方法  爱好  2年前 (2014-05-29)  7,601  8 当博客从华夏名网转移到阿里云之后,发现了不少问题,其中一个就是wordpress中文标签无 ...

  9. slogan

    nasa to infinity and beyond Werner Vogels at amazon all things distributed Kelly Johnson at Lockheed ...

  10. hdu2037-----------贪心, 活动安排问题

    http://acm.hdu.edu.cn/showproblem.php?pid=2037(简单贪心-----活动安排) #include<iostream> #include<a ...