128 Longest Consecutive Sequence 一个无序整数数组中找到最长连续序列
给定一个未排序的整数数组,找出最长连续序列的长度。
例如,
给出 [100, 4, 200, 1, 3, 2],
这个最长的连续序列是 [1, 2, 3, 4]。返回所求长度: 4。
要求你的算法复杂度为 O(n)。
详见:https://leetcode.com/problems/longest-consecutive-sequence/description/
Java实现:
方法一:
class Solution {
public int longestConsecutive(int[] nums) {
int res=0;
Set<Integer> s=new HashSet<Integer>();
for(int num:nums){
s.add(num);
}
for(int num:nums){
if(s.remove(num)){
int pre=num-1;
int next=num+1;
while(s.remove(pre)){
--pre;
}
while(s.remove(next)){
++next;
}
res=Math.max(res,next-pre-1);
}
}
return res;
}
}
方法二:
class Solution {
public int longestConsecutive(int[] nums) {
int res = 0;
Map<Integer, Integer> m = new HashMap<Integer, Integer>();
for (int num : nums) {
if (!m.containsKey(num)){
int pre = m.containsKey(num - 1) ? m.get(num - 1) : 0;
int next = m.containsKey(num + 1) ? m.get(num + 1) : 0;
int sum = pre + next + 1;
m.put(num, sum);
res = Math.max(res, sum);
m.put(num - pre, sum);
m.put(num + next, sum);
}
}
return res;
}
}
参考:https://www.cnblogs.com/grandyang/p/4276225.html
128 Longest Consecutive Sequence 一个无序整数数组中找到最长连续序列的更多相关文章
- LeetCode 128 Longest Consecutive Sequence 一个无序整数数组中找到最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.Fo ...
- Java算法-------无序数组中的最长连续序列---------leetcode128
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- [LeetCode] 128. Longest Consecutive Sequence 解题思路
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- [leetcode]128. Longest Consecutive Sequence最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Y ...
- [LeetCode] 128. Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- 128. Longest Consecutive Sequence(leetcode)
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- 【LeetCode】128. Longest Consecutive Sequence
Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...
- 128. Longest Consecutive Sequence *HARD* -- 寻找无序数组中最长连续序列的长度
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- 128. Longest Consecutive Sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
随机推荐
- 网页 H5“线条” 特效实现方式(canvas-nest)
先上图 (看博客空白处也可以呦): 前一阵浏览网站的时候,发现了这个好玩的东西,一直想找找怎么实现的,今天忙里偷闲,上网搜了一下,发现实现起来特别简单. 只需要在网页body里引入一个<scri ...
- IOS中UIAlertView(警告框)常用方法总结
一.初始化方法 - (instancetype)initWithTitle:(NSString *)title message:(NSString*)message delegate:(id /*&l ...
- VK Cup 2016 - Round 1 (Div. 2 Edition) C. Bear and Forgotten Tree 3
C. Bear and Forgotten Tree 3 time limit per test 2 seconds memory limit per test 256 megabytes input ...
- bzoj4105: [Thu Summer Camp 2015]平方运算
填坑 我不知道怎么算的,但是所有环的LCM数不会超过60 然后用线段树维护这个东西,每个节点记录子树内的循环节 没到循环节的暴力枚举 复杂度是nlogn再乘以循环节长度 #include<cst ...
- bzoj3134: [Baltic2013]numbers
稍微用脑子想一想,要是一个回文数,要么s[i]==s[i+1]要么s[i]==s[i+2]就可以实锤了 所以多开两维表示最近两位选的是什么数就完了 注意前导0 #include<cstdio&g ...
- ping 中的“TTL"是什么意思
简单来说就是表示一个数据包在网络中可以跳跃的结点数据,当该数据为零时本数据包将被抛弃 详细解释看以下引用: TTL (although named as "time" to liv ...
- 【前端】CentOS 7 系列教程之六: 安装 mysql 5.7
转载请注明出处:http://www.cnblogs.com/shamoyuu/p/linux_6.html 进入/usr/local/src目录 cd /usr/local/src 下载mysql的 ...
- [yii2]urlmanger
首先配置下nginx,确保可以不使用index.php来访问 server{ listen 8082; server_name yii2.dev; access_log logs/yii2.acces ...
- 替换一个文件中的内容BAT
@echo off setlocal enabledelayedexpansion set file=%1set "file=%file:"=%" for %%i in ...
- pkill详解
pkill详解 一:含义: 是ps命令和kill命令的结合,按照进程名来杀死指定进程,pkill和killall应用方法差不多,也是直接杀死运行中的程序:如果您想杀掉单个进程,请用kill来杀掉. 二 ...