[抄题]:

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. js之ActiveX控件使用说明 new ActiveXObject()

    什么是 ActiveX 控件? ActiveX 控件广泛用于 Internet.它们可以通过提供视频.动画内容等来增加浏览的乐趣.不过,这些程序可能出问题或者向您提供不需要的内容.在某些情况下,这些程 ...

  2. keras开发成sklearn接口

    我们可以通过包装器将Sequential模型(仅有一个输入)作为Scikit-Learn工作流的一部分,相关的包装器定义在keras.wrappers.scikit_learn.py中: 这里有两个包 ...

  3. CRM 2016 级联过滤 类比省市县

    以下以省市为例: function preFilterLookup() { //要进行过滤的lookup按钮加入addPresearch事件 Xrm.Page.getControl("shi ...

  4. ExtJS自定义事件

    1.开发ExtJS组件UI的时候,基本上对于一些操作,就是与后台交互之类的多数都是用户进行点击触发一个事件,在事件的处理器handler里面调具体的业务方法,完成业务数据的处理以及业务流程的流转机制, ...

  5. PHP流程控制 - if 语句

    PHP - if 语句 if 语句用于仅当指定条件成立时执行代码. 语法 if (条件) { 条件成立时要执行的代码; } 如果当前时间小于 20,下面的实例将输出 "Have a good ...

  6. AJAX相关总结

    AJAX即"Asynchronous Javascript And XML"(异步JavaScript和XML),是指一种创建交互式网页应用的网页开发技术. AJAX = 异步 J ...

  7. django中的 form 表单操作

     form组件  1. 能做什么事?   1. 能生成HTML代码  input框   2. 可以校验数据   3. 保留输入的数据   4. 有错误的提示   1. 定义   from django ...

  8. [Python] numpy.nonzero

    numpy.nonzero(a) Return the indices of the elements that are non-zero. Returns a tuple of arrays, on ...

  9. node实现缓存

    内容: 1.缓存基本原理 2.node实现缓存 1.缓存基本原理 第一重要.缓存策略: cache-control:用于控制缓存,常见的取值有private.no-cache.max-age.must ...

  10. linux装机首先需要关闭的服务

    关闭selinux和iptables:setenforce 0 iptables -Fiptables -t nat -Fsystemctl stop firewalldsystemctl disab ...