[抄题]:

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. 基于nginx和tengine的tcp反向代理,负载均衡 安装和配置

    先下载nginx_tcp_proxy_module模块. wget https://github.com/yaoweibin/nginx_tcp_proxy_module/archive/master ...

  2. Frequently Asked Questions

    转自:http://www.tornadoweb.org/en/stable/faq.html Frequently Asked Questions Why isn’t this example wi ...

  3. Hibernate inverse反转

    inverse: inverse: 指定由哪一方来维护之间的关联关系 false默认,表示不放弃,是主动放 true:表示把关联关系的维护反转(放弃),对集合对象的修改不会被反映到数据库中 容易出现的 ...

  4. 信息学奥赛(NOIP)初赛学习方法推荐

    首先声明:本帖针对初学者,本帖只是列出一个大概的框架,不属于自学方法,有条件有能力,请找一位好老师来教,多跟前辈交流经验.(否则多会出现事倍功半的悲剧!) 一.初赛内容 初赛偏重于基础知识. 1. 一 ...

  5. 进行web开发时应该考虑的架构性因素

    功能实现 这个自不必说. 性能与可伸缩性 根据预期的访问量,评估机器负载情况.如果在可预期的未来一台服务器可以撑得住,则没必要使用多台服务器.需要对多个环节进行性能评估:web服务器.逻辑服务器.DB ...

  6. python学习之----BeautifulSoup示例一

    BeautifulSoup 库最常用的对象恰好就是BeautifulSoup 对象. from urllib.request import urlopen from bs4 import Beauti ...

  7. mac mysql中文乱码问题

    God,今天看了好多资料,除了让我命令更熟练以外浪费了好多时间. 遇到的问题:写入数据库有中文的时候,显示??? 最后解决办法: 1.打开终端,输入: mysql -u root -p,然后输入mys ...

  8. python内置函数详细介绍

    知识内容: 1.python内置函数简介 2.python内置函数详细介绍 一.python内置函数简介 python中有很多内置函数,实现了一些基本功能,内置函数的官方介绍文档:    https: ...

  9. uva-10160-枚举

    若当前搜索到的城市n前面1-n-1编号的城市中有没有通电的,则永远也无法输送电力给那个城市,因为在剪枝时附加了和此结点连接的最大结点小于当前的结点 这段 for(int i = 1; i < c ...

  10. JAVA Spring 事物 ( 已转账为例 ) 基于 XML 配置,事务类型说明

    < 1 > 配置文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=&q ...