128. Longest Consecutive Sequence最长连续序列
[抄题]:
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.
[思维问题]:
以为要用好几个同一种数据结构来实现:其实不用,只要对同一个数自增、自减就可以了。
[一句话思路]:
求出up的上限,down的下限后作差、减1
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 如果集合中查有此数,则把它删掉。否则会导致溢出
[二刷]:
- down up要一直变化,所以要用到2个while小循环
[三刷]:
[总结]:
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[英文数据结构,为什么不用别的数据结构]:
hashmap:没有k-v的对应关系
array:找一次down就要for循环,写起来太麻烦
linkedlist:找一次down就要head-tail循环,写起来太麻烦
直接用hashset.contains判断存在性,避免了从头到尾地查找
[其他解法]:
[Follow Up]:
[题目变变变]:
public class Solution {
/*
* @param num: A list of integers
* @return: An integer
*/
public int longestConsecutive(int[] num) {
//corner case
if (num == null || num.length == 0) {
return 0;
}
//add
HashSet<Integer> set = new HashSet<Integer>();
for (int i = 0; i < num.length; i++) {
set.add(num[i]);
}
//find
int max = 0;
for (int i = 0; i < num.length; i++) {
int down = num[i] - 1;
while (set.contains(down)) {
set.remove(down);
down--;
} int up = num[i] + 1;
while (set.contains(up)) {
set.remove(up);
up++;
}
max = Math.max(max, up - down - 1);
} return max;
}
}
128. Longest Consecutive Sequence最长连续序列的更多相关文章
- [leetcode]128. Longest Consecutive Sequence最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Y ...
- 298. Binary Tree Longest Consecutive Sequence最长连续序列
[抄题]: Given a binary tree, find the length of the longest consecutive sequence path. The path refers ...
- [Leetcode] 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.Fo ...
- [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 一个无序整数数组中找到最长连续序列
给定一个未排序的整数数组,找出最长连续序列的长度.例如,给出 [100, 4, 200, 1, 3, 2],这个最长的连续序列是 [1, 2, 3, 4].返回所求长度: 4.要求你的算法复杂度为 O ...
- [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
Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...
- 128. Longest Consecutive Sequence(leetcode)
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
随机推荐
- [UE4]创建对象的的几种姿势(C++)
DEMO源代码 这个DEMO演示了在C++代码中,创建UE4的常见类型的对象,包括Actor,ActorComponent,加载资源等. 源代码请从这里下载:https://code.csdn.net ...
- [UE4]使用PlayerController获取鼠标点击时的坐标
1,获取鼠标在当前场景中坐标系统的中的position,加入场景地图的范围是一千平方米,那么这个position的范围也是1000米x1000米. 注册鼠标事件 FInputActionBinding ...
- Linux系统文件名字体不同的颜色都代表什么
Linux系统文件名字体不同的颜色都代表什么 在Linux中,文件的颜色都是有含义的. 其中, Linux中文件名颜色不同,代表文件类型不一样. 如下所示: www.2cto.com ...
- linux如何查看系统是多少位的?64 OR 32
1.可以用命令“getconf LONG_BIT”查看, 如果返回的结果是32则说明是32位,返回的结果是64则说明是64位. 2.此外还可以使用命令“uname -a”查看, 输出的结果中,如果有x ...
- javascript面向对象之Object.defineProperty(a,b,c)
/* Object.defineProperty(a,b,c);介绍 a:需要属性设置的对象 b:需要设置的属性名,(键值) c:是一个用于描述属性值得json数据.这个json数据有configur ...
- javascript的面向对象思想知识要点
获取数据类型 typeof undefined:访问某个不存在的或未经赋值的变量时就会得到一个 undefined,用typeof 获取类型,得到的也是undefined;null:它不能通过java ...
- JavaScript语句和异常
知识内容: 1.条件语句(分支语句) 2.循环语句 3.with语句 4.异常处理 5.本节练习 参考资料:<JavaScript高级程序设计> 1.条件语句 JavaScript中的条件 ...
- (10/24) 图片跳坑大战--处理html中的图片
补充,在前面的服务启动执行命令中,我们在package.json中的配置信息为: "scripts": { "server": "webpack-de ...
- bean-json-bean-json 工具
package com.taotao.utils; import java.util.List; import com.fasterxml.jackson.core.JsonProcessingExc ...
- delphi 泛型 c++builder 泛型
delphi 泛型 System.Generics.Collections.pas TList<T> http://docwiki.embarcadero.com/Libraries/Be ...