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 ...
随机推荐
- docker 基础操作
1. 安装docker 系统centos 7.2 yum -y install docker-io service docker start 安装完毕后执行 docker version 或者dock ...
- 【Codeforces】CF 5 C Longest Regular Bracket Sequence(dp)
题目 传送门:QWQ 分析 洛谷题解里有一位大佬讲的很好. 就是先用栈预处理出可以匹配的左右括号在数组中设为1 其他为0 最后求一下最长连续1的数量. 代码 #include <bits/std ...
- C#中char空值的几种表示方式
C#中char空值的几种表示方式 在C#中char类型的表示方式通常是用单引号作为分隔符,而字符串是用双引号作为分隔符. 例如: 程序代码 程序代码 char a = 'a'; char b = 'b ...
- Oracle 统计量NO_INVALIDATE参数配置(下)
转载:http://blog.itpub.net/17203031/viewspace-1067620/ 本篇我们继续讨论NO_INVALIDATE参数. 从上篇(http://blog.itpub. ...
- Mybatis通过colliection属性递归获取菜单树
1.现有商品分类数据表category结构如下,三个字段都为varchar类型 2.创建商品分类对应的数据Bean /** * */ package com.xdw.dao; import java. ...
- 在windows 下将 chm 格式的文件 转换成 html 的文件
有时我们可能需要将 chm 格式的文件 转换成 html 格式的网页文件,这时,如果你使用的是 windows 操作系统,那就可以用 windows 操作系统自带的反编译工具来完成这项任务,具体步骤: ...
- mysql5.6下载&安装
官网下载即可: mysql-installer-community-5.6.25.0.msi 安装: 1. 2. 3.在这里我们需要从安装程序提供的可安装的产品(Products)中选择我们需要的my ...
- 温故而知新-正则单词和strlen
1 正则表达式用\b表示单词的开始和结束 \bblog\b 正则查找blog这个单词 2 关于strlen的汉字问题 在utf8格式下 strlen('汉字')=6 在gbk格式下 strlen(' ...
- vsphere使用笔记
一,安装虚拟机,需要使用vsphere client上传镜像,提示错误: vsphere client 上传文件:Failed to log into NFC Server , 解决办法:用浏 ...
- gevent 实现io自动切换,gevent.join([]), gevent.spawn, 爬虫多并发的实现
gevent 是一个第三方库,可以很容易的实现遇到io(文件传输)操作时,程序自动跳转到下一个程序 例一: 用gevent.sleep() 来模拟io操作 import gevent def foo ...