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. 【转载】C++知识库内容精选 尽览所有核心技术点

    原文:C++知识库内容精选 尽览所有核心技术点 C++知识库全新发布. 该知识库由C++领域专家.CSDN知名博客专家.资深程序员和项目经理安晓辉(@foruok)绘制C++知识图谱,@wangshu ...

  2. 不小心删除数据--利用MySQL的binlog恢复数据

    MySQL Binary Log也就是常说的bin-log, ,是mysql执行改动产生的二进制日志文件,其主要作用有两个: * 数据回复 * 主从数据库.用于slave端执行增删改,保持与maste ...

  3. git sshkeygen Fingerprint cannot be generated解决方法

    ssh-keygen -t rsa -C "xxx@xxx.com"   生成后使用cat或者vim 查看该rsa,然后复制到github的ssh keys中:     提示:   ...

  4. MyEclipse中文乱码解决方法

    在Myeclipse导入一个项目,有中文乱码问题,解决方法如下: 一.将整个project设置编码UTF-8(UTF-8可以最大的支持国际化) windows->Preferences-> ...

  5. mysql概要(四)order by,group 的特点,子查询

    1.order by 默认按升序排列(asc/desc),多字段排序 order by 字段 排序方式,字段2 排序方式,..:在分组排序中,排序是对分组后的结局进行排序,而不是在组中进行排序. 2. ...

  6. Linux_查看进程

    1. 静态: ps -aux 2. 动态: top 3.

  7. 原生js实现的效果

    原生js实现tooltip提示框的效果   在js的世界里面,每一个小的特效都那么微不足道,却又那么的令人向往与好奇.前端工程师的任务特别高大上,因为他们的一个小小的设计就会激发别人的求知欲.比如说我 ...

  8. (五)Super VLAN

  9. [js] 有趣的js面试题,你答对了吗?

    题目1: 回答以下代码,alert的值分别是多少?

  10. The Zen Programmer (zhuan)

    http://blog.csdn.NET/marksinoberg/article/details/52460725 ***************************************** ...