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 ...
随机推荐
- Debian8 远程登录Permission Denied,please try again
多数是系统没有开启Root登录的权限. 修改root的ssh权限: ① vi /etc/ssh/sshd_config ② 找到配置项PermitRootLogin 将此项的值改为yes ③ 重启ss ...
- Pre-Query trigger in Oracle D2k / Oracle Forms
Pre-Query trigger in Oracle D2k / Oracle Forms DescriptionFires during Execute Query or Count Query ...
- sql 基础练习题
select * from Student;select * from Courseselect ;select* from Teacherselect ;select * from SC;--创建测 ...
- [SAP ABAP开发技术总结]结构复用(INCLUDE)
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- HDU 1005 Number Sequence(数列)
HDU 1005 Number Sequence(数列) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
- JQery 中的 $(".bb:eq(1)") eq () 解释。。
这是一段代码: <div id="bb">a</div> <div id="bb">b</div> <di ...
- 转 Cocos网络篇[3.2](3) ——Socket连接(1)
Cocos网络篇[3.2](3) ——Socket连接(1) 2015-03-05 22:24:13 标签:network http socket cocos [唠叨] 在客户端游戏开发中,使用HTT ...
- DB层面上的设计 分库分表 读写分离 集群化 负载均衡
第1章 引言 随着互联网应用的广泛普及,海量数据的存储和访问成为了系统设计的瓶颈问题.对于一个大型的 互联网应用,每天几十亿的PV无疑对数据库造成了相当高的负载.对于系统的稳定性和扩展性造成了极大的 ...
- Scrum Meeting---Four(2015-10-28)
今日已完成任务和明日要做的任务 姓名 今日已完成任务 今日时间 明日计划完成任务 估计用时 董元财 今日我完成了数据库表的设计以及创建 3h 进行Java Web工程的编写 4h 胡亚坤 用户之间的通 ...
- iOS - UISlider
前言 NS_CLASS_AVAILABLE_IOS(2_0) __TVOS_PROHIBITED @interface UISlider : UIControl <NSCoding> @a ...