最长连续序列

题目链接:LeetCode128

描述

给定一个未排序的整数数组 nums ,找出数字连续的最长序列(不要求序列元素在原数组中连续)的长度。

请你设计并实现时间复杂度为 O(n) 的算法解决此问题。

示例

输入:nums = [100,4,200,1,3,2]

输出:4

解释:最长数字连续序列是 [1, 2, 3, 4]。它的长度为 4。

思路

1、用一个哈希表存储数组中的数。

2、遍历哈希表,查找当前的数前面一个数是否存在,即num-1在哈希表里面是否存在,如果存在这个数,那么当前的num肯定不是最长连续序列的第一位数,直接跳过。

3、如果不存在,那么计算以num开头的连续序列的长度,直接判断num++是否存在,存在则长度curLen++。

4、将得到的连续序列的长度与前一个连续序列的长度比较,取大的那一个,不断更新最长连续序列的长度,最后返回。

代码

class Solution {
public int longestConsecutive(int[] nums) {
Set<Integer> set = new HashSet<>();
for(int num : nums){
set.add(num);
}
int result = 0;
for(int num : set){
if(!set.contains(num-1)){
int curLen = 0;
int curNum = num;
while(set.contains(curNum++)){
curLen += 1;
}
result = Math.max(result,curLen);
}
}
return result;
}
}

LeetCode128 最长连续序列的更多相关文章

  1. [Swift]LeetCode128. 最长连续序列 | Longest Consecutive Sequence

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

  2. [leetcode-128] 最长连续序列

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

  3. [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

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

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

  5. lintcode: 最长连续序列

    最长连续序列 给定一个未排序的整数数组,找出最长连续序列的长度. 说明 要求你的算法复杂度为O(n) 样例 给出数组[100, 4, 200, 1, 3, 2],这个最长的连续序列是 [1, 2, 3 ...

  6. [LeetCode] Binary Tree Longest Consecutive Sequence II 二叉树最长连续序列之二

    Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree. Especia ...

  7. [Swift]LeetCode298. 二叉树最长连续序列 $ Binary Tree Longest Consecutive Sequence

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

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

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

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

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

  10. lintcode-124-最长连续序列

    124-最长连续序列 给定一个未排序的整数数组,找出最长连续序列的长度. 说明 要求你的算法复杂度为O(n) 样例 给出数组[100, 4, 200, 1, 3, 2],这个最长的连续序列是 [1, ...

随机推荐

  1. 搜索引擎用法 cheatsheet

    逻辑 写法 与 keyword1 keyword2 或 keyword1 OR keyword2 限定关键词的排列 "keyword" 限定搜索的网站 site:cnblogs.c ...

  2. redis zset 使用场景

    前文,我们讨论过redis 的数据结构及使用场景.可参考: 参考: 总结篇4:redis 核心数据存储结构及核心业务模型实现应用场景 https://www.cnblogs.com/yizhiamum ...

  3. SQL全表扫描优化基础知识

    1.模糊查询效率很低: 原因:like本身效率就比较低,应该尽量避免查询条件使用like:对于like '%...%'(全模糊)这样的条件,是无法使用索引的,全表扫描自然效率很低:另外,由于匹配算法的 ...

  4. AI将诗意装进口袋!合合信息扫描全能王“扫描书籍”功能优化上线

    人间最美四月天,正是读书好时节.4月20日至23日,正值"世界读书日"之际,合合信息旗下扫描全能王联合上海首家图像小说主题书店--安古莱姆书店,共同发起"春天正是读书天& ...

  5. CSS – background and styling img

    前言 之前写过一些: W3Schools 学习笔记 (2) – CSS Image Sprites W3Schools 学习笔记 (3) – CSS Styling Images & CSS ...

  6. iPay88 学习笔记

    ipay88 学习笔记 之前弄过 MOLPay 现在弄 ipay88</p><p>差不多的概念 这里记入流程就好了 首先是做订单, 然后通过 merchant key + 订单 ...

  7. duxapp:基于Taro使用模块化开发,提升开发效率

    duxapp是基于Taro二次开发的模块化框架 使用这个框架,结合框架提供的UI库和工具库,能帮助你快速且高质量的完成项目,且能实现同时开发小程序.H5.APP(React Native),并且保证各 ...

  8. gaussian噪声

    高斯噪声 高斯噪声(Gaussian noise)是一种具有正态分布(也称作高斯分布)概率密度函数的噪声.换句话说,高斯噪声的值遵循高斯分布或者它在各个频率分量上的能量具有高斯分布.它被极其普遍地应用 ...

  9. 自签openssl证书(包含泛域名)

    1.安装openSSL weget http://www.openssl.org/source/openssl-1.0.0a.tar.gz Tar -zxvf openssl-1.0.0a.tar.g ...

  10. 征服 Docker 镜像访问限制:KubeSphere v3.4.1 成功部署全攻略

    近期,KubeSphere 社区的讨论中频繁出现关于 Docker 官方镜像仓库访问受限的问题. 本文旨在为您提供一个详细的指南, 展示在 Docker 官方镜像访问受限的情况下,如何通过 KubeK ...