Longest Consecutive Sequence [LeetCode]
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]的更多相关文章
- 128. Longest Consecutive Sequence(leetcode)
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- Binary Tree Longest Consecutive Sequence -- LeetCode
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- Longest Consecutive Sequence——Leetcode
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- Longest Consecutive Sequence leetcode java
题目: Given an unsorted array of integers, find the length of the longest consecutive elements sequenc ...
- [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- [LeetCode] Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- LeetCode Binary Tree Longest Consecutive Sequence
原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/ 题目: Given a binary t ...
- 【LeetCode OJ】Longest Consecutive Sequence
Problem Link: http://oj.leetcode.com/problems/longest-consecutive-sequence/ This problem is a classi ...
- [LeetCode] 128. Longest Consecutive Sequence 解题思路
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
随机推荐
- MySQL(六) —— 自定义函数
自定义函数 用户自定义函数(user-defined function, UDF)是一种对MySQL扩展的途径,其用法与内置函数相同. 参数,返回值 创建自定义函数 CREATE FUNCTION f ...
- netstat -ano,查看已占用端口,结束已被占用的端口,ntsd,关闭任务管理器杀不了的进程
cmd——回车,输入netstat -ano——回车,可以查看已占用的端口,记下端口的PID,然后打开任务管理器,点查看,选择列,勾选PID确定,找到对应的PID,结束进程,如果结束不了或者结束后还不 ...
- 关于STM32库中 __IO 修饰符(volatile修饰符,反复无常的意思)
STM32例子代码中会有像这样的代码 static __IO uint32_t TimingDelay; 这里边的__IO修饰符不好理解,单从字面可以看出是为IO相关,查其标准库可以得知这个__IO ...
- Codeforces Round #288 (Div. 2) C. Anya and Ghosts 模拟
C. Anya and Ghosts time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 《FLASH CC 2015 CANVAS 中文教程》——2、基本的交互(点击、触摸)事件
注::如果你对 FLASH 这个软件操作不够熟悉,建议你可以先看看FLASH动画之类的书. :FLASH CC 在文中直接简称为CC. :以下所以文章中所说的快捷键 如果你按了不起作用,请检查是否有其 ...
- javascript练习----复选框全选,全不选,反选
第一种方式: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...
- iOS - UITextField
前言 NS_CLASS_AVAILABLE_IOS(2_0) @interface UITextField : UIControl <UITextInput, NSCoding> @ava ...
- HDU5807分段dp
DAG图. [题意] n(50)个城市m(c(n,2))条单向边(x,y),保证x<y 对于三个点(x,y,z)如果abs(w[x]-w[y])<=K && abs(w[x ...
- 自学EF一些小笔记
一直在用DHhelper做MVC,感觉好山寨,也不怎么好用.决定开始学EF. 废话不多说开始记笔记..... EF就是把数据库表,存储过程,视图实例化,通过继承DbContext的一个类来操作数据实例 ...
- 用命令访问D:\python学习\wendjia教程\aa.py
用命令访问D:\python学习\wendjia教程\aa.py d: -----------切换到D盘 cd python学习\wend ...