[抄题]:

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

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 如果集合中查有此数,则把它删掉。否则会导致溢出

[二刷]:

  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最长连续序列的更多相关文章

  1. [leetcode]128. Longest Consecutive Sequence最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Y ...

  2. 298. Binary Tree Longest Consecutive Sequence最长连续序列

    [抄题]: Given a binary tree, find the length of the longest consecutive sequence path. The path refers ...

  3. [Leetcode] Longest consecutive sequence 最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  4. LeetCode 128 Longest Consecutive Sequence 一个无序整数数组中找到最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence.Fo ...

  5. [LeetCode] 128. Longest Consecutive Sequence 求最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  6. 128 Longest Consecutive Sequence 一个无序整数数组中找到最长连续序列

    给定一个未排序的整数数组,找出最长连续序列的长度.例如,给出 [100, 4, 200, 1, 3, 2],这个最长的连续序列是 [1, 2, 3, 4].返回所求长度: 4.要求你的算法复杂度为 O ...

  7. [LeetCode] 128. Longest Consecutive Sequence 解题思路

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  8. 【LeetCode】128. Longest Consecutive Sequence

    Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...

  9. 128. Longest Consecutive Sequence(leetcode)

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

随机推荐

  1. Linux故障-bash-4.1$

    #模拟故障:-bash-4.1$ [root@nodchen ~]# su - cisco[cisco@nodchen ~]$ \rm -f .*rm: cannot remove `.': Is a ...

  2. PHP PDO prepare()、execute()和bindParam()方法详解

    每次将查询发送给MySQL服务器时,都必须解析该查询的语法,确保结构正确并能够执行.这是这个过程中必要的步骤,但也确实带来了一些开销.做一次是必要的,但如果反复地执行相同的查询,批量插入多行并只改变列 ...

  3. HttpURLConnection连接超时问题

    1.问题描述 这几天测试重构后的下载框架,发现在下载过程中如果网络中断或网络较差,个别应用的下载就会阻塞卡住,一直卡在 “正在下载 xx%”.   2.问题排查和定位 思考:网络差不应该报网络异常的错 ...

  4. spark-submit 提交Application

    在spark1.0中推出spark-submit来统一提交applicaiton ./bin/spark-submit \ --class <main-class> --master &l ...

  5. windows 下,查看并杀死进程

    今天启动我的play framework 服务 提示 could not bind on 9000.还是个error.这让我很不解,昨天还好好的. 怀疑是9000呗某个服务占了,在linux下还挺好办 ...

  6. Java技术栈

    内容: 1.Java基础(JavaSE) 2.数据结构与算法与设计模式 3.计算机理论知识 4.数据库 5.Java web(JavaEE) 6.消息队列 7.Linux及服务器相关 8.分布式相关 ...

  7. Java-IntegerCache

    Integer类里面有一个私有的静态内部类IntegerCache类加载时,有一段静态块代码,如下 static final int low = -128; static final int high ...

  8. linux添加计划任务

    crond 是linux用来定期执行程序的命令.当安装完成操作系统之后,默认便会启动此任务调度命令.crond命令每分锺会定期检查是否有要执行的工作,如果有要执行的工作便会自动执行该工作.可以用以下的 ...

  9. getitem, setitem, delitem (把类实例化成字典的类型)

    class Foo(object):     def __init__(self):         self.data = {} def __getitem__(self, key):        ...

  10. 9 random模块

    1.ramdom模块主要方法 random.random()函数是这个模块中最常用的方法了,它会生成一个随机的浮点数,范围是在0.0~1.0之间. random.uniform()正好弥补了上面函数的 ...