前言:

每道题附带动态示意图,提供java、python两种语言答案,力求提供leetcode最优解。

描述:

给定一个未排序的整数数组,找出最长连续序列的长度。

要求算法的时间复杂度为 O(n)。

示例:

输入: [100, 4, 200, 1, 3, 2]
输出: 4
解释: 最长连续序列是 [1, 2, 3, 4]。它的长度为 4。

思路:

  首先,我们先来看一个简单的例子:序列123 序列 56,插入4,求连续序列长度。

  

  很容易得出结论,答案是6,那么这个6是怎么来的呢?6 = len(1,2,3) + len(5,6) + 1。

  当4插入的时候,我们关心的是3,5,而当序列1,2,3,4,5,6一旦形成,那么元素2,3,4,5我们将不会关心,因为无论新加入序列的元素取何值,都不会和元素2,3,4,5组合成连续序列。

  那么我们是否可以将连续序列的长度,当作一种状态,绑定在连续序列的首尾元素上?

此时,当元素7插入序列后,组成的最大连续序列就是4 = 2+1+1,因此我们可以分析出,该问题的dp方程是 dp[i] = dp[i - 1] + dp[i+1]

我们可以借助散列表来简化时间复杂度。

图解:

java:

  1. class Solution {
  2. public int longestConsecutive(int[] nums) {
  3. int len = nums.length;
  4. int max = 0;
  5. if (len == 0) {
  6. return max;
  7. }
  8. HashMap<Integer, Integer> map = new HashMap<>(len);
  9. for (int i = 0; i < len; i++) {
  10. if (!map.keySet().contains(nums[i])) {
  11. Integer before = map.getOrDefault(nums[i] - 1, 0);
  12. Integer after = map.getOrDefault(nums[i] + 1, 0);
  13. int value = before + after + 1;
  14. max = Math.max(value, max);
  15.  
  16. map.put(nums[i] - before, value);
  17.  
  18. map.put(nums[i] + after, value);
  19. }
  20. }
  21. return max;
  22. }
  23. }

结果:

python:

  1. class Solution(object):
  2. def longestConsecutive(self, nums):
  3. hash_dict = dict()
  4.  
  5. max_length = 0
  6. for num in nums:
  7. if num not in hash_dict:
  8. left = hash_dict.get(num - 1, 0)
  9. right = hash_dict.get(num + 1, 0)
  10.  
  11. cur_length = 1 + left + right
  12. if cur_length > max_length:
  13. max_length = cur_length
  14.  
  15. hash_dict[num] = cur_length
  16. hash_dict[num - left] = cur_length
  17. hash_dict[num + right] = cur_length
  18.  
  19. return max_length

结果:

图解leetcode —— 128. 最长连续序列的更多相关文章

  1. Java实现 LeetCode 128 最长连续序列

    128. 最长连续序列 给定一个未排序的整数数组,找出最长连续序列的长度. 要求算法的时间复杂度为 O(n). 示例: 输入: [100, 4, 200, 1, 3, 2] 输出: 4 解释: 最长连 ...

  2. LeetCode 128. 最长连续序列(Longest Consecutive Sequence)

    题目描述 给定一个未排序的整数数组,找出最长连续序列的长度. 要求算法的时间复杂度为 O(n). 示例: 输入: [100, 4, 200, 1, 3, 2] 输出: 4 解释: 最长连续序列是 [1 ...

  3. leetcode 128最长连续序列

    方法一:使用快排: //排序法,时间O(nlogn),使用STL,只是验证一下思想,非正解: class Solution { public: int longestConsecutive(vecto ...

  4. 【LeetCode】128. 最长连续序列

    题目 给定一个未排序的整数数组,找出最长连续序列的长度. 要求算法的时间复杂度为O(n). 示例: 输入:[100, 4, 200, 1, 3, 2] 输出:4 解释:最长连续序列是[1, 2, 3, ...

  5. leetcode.哈希表.128最长连续序列-Java

    1. 具体题目 给定一个未排序的整数数组,找出最长连续序列的长度.要求算法的时间复杂度为 O(n). 示例: 输入: [100, 4, 200, 1, 3, 2] 输出: 4 解释: 最长连续序列是 ...

  6. leetcode 128. 最长连续子序列

    题目描述: 给定一个未排序的整数数组,找出最长连续序列的长度. 要求算法的时间复杂度为 O(n). 示例: 输入:[100, 4, 200, 1, 3, 2] 输出:4 即最长的连续序列为 [1,2, ...

  7. 【LeetCode】最长连续序列

    [问题]给定一个未排序的整数数组,找出最长连续序列的长度. 要求算法的时间复杂度为 O(n). 示例: 输入: [, , , , , ] 输出: 解释: 最长连续序列是 [, , , ].它的长度为 ...

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

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

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

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

随机推荐

  1. vue 优化小技巧 之 require.context()

    1.require.context() 回忆一下 当我们引入组件时 第一步 创建一个子组件 第二步 import ... form ... 第三步 components:{..} 第四步 页面使用 & ...

  2. 2019-9-9:渗透测试,基础学习,phpmyadmin getshell方法,基于时间的盲注,基于报错的注入,笔记

    phpmyadmin getshell方法1,查看是否有导入导出设置 show global variables like '%secure-file-priv%';2,如果secure-file-p ...

  3. 模型量化原理及tflite示例

    模型量化 什么是量化 模型的weights数据一般是float32的,量化即将他们转换为int8的.当然其实量化有很多种,主流是int8/fp16量化,其他的还有比如 二进制神经网络:在运行时具有二进 ...

  4. Anaconda中启动Python时的错误:UnicodeDecodeError: 'gbk' codec can't decode byte 0xaf in position 553

    今天,在Anaconda prompt启动python遇到了如下错误: UnicodeDecodeError: ‘gbk’ codec can’t decode byte 0xaf in positi ...

  5. Linux I/O复用 —— epoll 部分源码剖析

    epoll 相关的系统调用有以下三个,这里简述下当调用对应函数后,内核的具体实现 epoll_creat( ) 在内核注册文件系统 eventpollfs,挂载此文件系统 (linux一切皆文件,便于 ...

  6. zabbix自动发现 url 以及对http返回状态码监控实现 告警

    2019-06-04 18:39:12 目的:批量监控业务URL的返回状态码,通过zabbix监控判断业务好坏. 1.zabbix安装 请查看此永久链接:https://www.cnblogs.com ...

  7. 【Android - 进阶】之RemoteViews简介

    RemoteViews,顾名思义,就是远程的View,也就是可以运行在其他进程中的View.RemoteViews常用在通知和桌面小组件中. 一.RemoteViews应用到通知 首先来介绍一下系统自 ...

  8. SpringCloud Alibaba微服务实战三 - 服务调用

    导读:通过前面两篇文章我们准备好了微服务的基础环境并让accout-service 和 product-service对外提供了增删改查的能力,本篇我们的内容是让order-service作为消费者远 ...

  9. mybatis精讲(五)--映射器组件

    目录 前言 标签 select insert|update|delete 参数 resultMap cache 自定义缓存 # 加入战队 微信公众号 前言 映射器之前我们已经提到了,是mybatis特 ...

  10. Grok Debugger安装配置

    前言:由于使用ELK对日志进行集中管理,grok表达式无法验证是否正确,所以使用Grok Debugger进行调试,但是由于国外网站上不去(http://grokdebug.herokuapp.com ...