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. window server开发

    代码部分: public partial class tv : ServiceBase { public tv() { InitializeComponent(); ServiceName = &qu ...

  2. Dbcp2抛出org.apache.commons.dbcp2.LifetimeExceededException

    三月 24, 2016 5:16:33 下午 org.apache.commons.dbcp2.BasicDataSource onSwallowException 警告: An internal o ...

  3. [转]SIP穿越NAT&FireWall解决方案

    原文链接(也是转载)http://blog.csdn.net/yetyongjin/article/details/6881491.我修改了部分错字.   SIP从私网到公网会遇到什么样的问题呢? 1 ...

  4. Java_JDK_TreeMap

    (一)TreeMap TreeMap使用的是红黑树来实现的,所以重点是红黑树的插入和删除. 红黑树的3个特性: 根节点和所有外部节点的颜色都是黑色的: 从根节点到外部节点的途中没有连续两个节点的颜色是 ...

  5. MeshLab中进行点云配准

    MeshLab是一个开源.可移植和可扩展的三维几何处理系统,主要用于交互处理和非结构化编辑三维三角形网格.它支持多种文件格式: import:PLY, STL, OFF, OBJ, 3DS, COLL ...

  6. [C程序设计语言]第二部分

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  7. POJ 1979 Red and Black (红与黑)

    POJ 1979 Red and Black (红与黑) Time Limit: 1000MS    Memory Limit: 30000K Description 题目描述 There is a ...

  8. U盘制作Ubuntu15.04启动盘失败

    先用ubuntu15.04光盘在已有xp的电脑上安装成功​ 随后在Ubuntu安装labview说glibc没安装​ 但是ldd --version显示是安装的新版的​ 后来怀疑是86_64的原因​ ...

  9. php xml 操作。

    参考 文章:http://www.cnblogs.com/zcy_soft/archive/2011/01/26/1945482.html DOMDocument相关的内容. 属性: Attribut ...

  10. iOS - Swift NSTimeZone 时区

    前言 public class NSTimeZone : NSObject, NSCopying, NSSecureCoding NSTimeZone 表示时区信息. 1.NSTimeZone 时区的 ...