前言:

每道题附带动态示意图,提供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:

class Solution {
public int longestConsecutive(int[] nums) {
int len = nums.length;
int max = 0;
if (len == 0) {
return max;
}
HashMap<Integer, Integer> map = new HashMap<>(len);
for (int i = 0; i < len; i++) {
if (!map.keySet().contains(nums[i])) {
Integer before = map.getOrDefault(nums[i] - 1, 0);
Integer after = map.getOrDefault(nums[i] + 1, 0);
int value = before + after + 1;
max = Math.max(value, max); map.put(nums[i] - before, value); map.put(nums[i] + after, value);
}
}
return max;
}
}

结果:

python:

class Solution(object):
def longestConsecutive(self, nums):
hash_dict = dict() max_length = 0
for num in nums:
if num not in hash_dict:
left = hash_dict.get(num - 1, 0)
right = hash_dict.get(num + 1, 0) cur_length = 1 + left + right
if cur_length > max_length:
max_length = cur_length hash_dict[num] = cur_length
hash_dict[num - left] = cur_length
hash_dict[num + right] = cur_length 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. H3C交换机、路由器 ssh登录配置

    VLAN 10  创建vlan并配好ip inter vlan 10 ip add  20.1.1.1  24 qu ip route-static 0.0.0.0 0 20.1.1.254  写好静 ...

  2. Stream系列(二)Map方法使用

    Stream 将List 里面的对象转换为新的对象 EmployeeTestCase.java package com.example.demo; import lombok.Data; import ...

  3. Centos 7.x 系统基础优化

    Centos 7.x 系统基础优化 1.更换国内yum源 删除系统带的centos官方yum源 rm -rf /etc/yum.repos.d/* 使用国内阿里云源 curl -o /etc/yum. ...

  4. Chapter 02—Creating a dataset(Part3-补充材料Stat/Transfer)

    Stat/Transfer:在电子表格(worksheet),数据库(database),统计包(statistical package)间进行数据转换,具有简单高效的特点. 资料来源于:http:/ ...

  5. git的用法 回到某个版本

    进入到项目文件夹 如果新建项目时没有勾选git 进入到项目中

  6. mysql客户端 navicat 本地导入sql文件出错

    以前遇到过这个问题,找了半天度娘没解决,然后就放弃了. 因为是自己 demo 的项目 所以就自己手动建表了. 现在实习了,去到公司下载下代码来,拿上sql 导入发现还是报错, 根本没法整,然后自己都不 ...

  7. js人民币转大写

    <input type="text" oninput="OnInput (event)" value="1234567"> &l ...

  8. css练习——两列左窄右kuan型

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  9. 大型情感剧集Selenium:1_介绍 #华为云·寻找黑马程序员#

    学习selenium能做什么? 很多书籍.文章中是这么定义selenium的: Selenium 是开源的自动化测试工具,它主要是用于Web 应用程序的自动化测试,不只局限于此,同时支持所有基于web ...

  10. 使用正则表达式实现(加减乘除)计算器(C#实现)

    起因:公司领导要求做一款基于行业规范的计算器, 然后需要用户输入一些数据,然后根据用户输入的数据满足某些条件后,再根据用户输入的条件二进行加减乘除运算.;-) 期间因为查找规范等形成数据表的某一列是带 ...