【LeetCode】最长连续序列
【问题】给定一个未排序的整数数组,找出最长连续序列的长度。
要求算法的时间复杂度为 O(n)。
示例:
输入: [, , , , , ]
输出:
解释: 最长连续序列是 [, , , ]。它的长度为 。
【思路】
首先使用一个哈希set将我们的数据全都保存,然后遍历整个数组,假如遍历到了数字A,其一定在哈希set中,这是毋庸置疑的。接着我们需要进入一个while循环去判断A+1,A+2,A+3…是不是也在这个哈希表中,如果尝试到数字B,其不在哈希表中则退出,此时最长连续序列B-A。
既然我们查找过了A+1,A+2,A+3, 其在哈希表中,那么我们遍历数组的时候要需要遍历这些值么?显然不需要,因此我们可以优化一下,什么时候才需要进入上述过程!
当某一个数的前一个数没有在数组,也就是没有在哈希set中,我们就从这个数字开始遍历,统计到的连续序列一定是该部分最长的!!!
class Solution {
public:
int longestConsecutive(vector<int>& nums) {
if(nums.size() == ) return ;
unordered_set<int> mySet(nums.begin(), nums.end());
int res = ;
for(auto num: nums){
if(mySet.count(num - ) == ){
int tmp = num; //保存初始数据
while(mySet.count(tmp)){
tmp++;
}
res = max(res, tmp-num);
}
}
return res;
}
};
【LeetCode】最长连续序列的更多相关文章
- Leetcode 最长连续序列
题目链接:https://leetcode-cn.com/problems/longest-consecutive-sequence/ 题目大意: 略. 分析: 注意有重复值,序列为空等情况. 代码如 ...
- leetcode 最长连续序列 longest consecutive sequence
转载请注明来自souldak,微博:@evagle 题目(来自leetcode): 给你一个n个数的乱序序列,O(N)找出其中最长的连续序列的长度. 例如给你[100, 4, 200, 1, 3, 2 ...
- [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- [LeetCode] Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- [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 ...
- 图解leetcode —— 128. 最长连续序列
前言: 每道题附带动态示意图,提供java.python两种语言答案,力求提供leetcode最优解. 描述: 给定一个未排序的整数数组,找出最长连续序列的长度. 要求算法的时间复杂度为 O(n). ...
- [LeetCode] 128. Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- [LeetCode] 298. Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- [LeetCode] 549. Binary Tree Longest Consecutive Sequence II 二叉树最长连续序列之 II
Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree. Especia ...
- [leetcode]128. Longest Consecutive Sequence最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Y ...
随机推荐
- 【Go语言系列】2.2、Go语言基本程序结构:关键字与标识符
什么是标识符 标识符用来命名变量.类型等程序实体.标识符是指Go语言对各种变量.方法.函数等命名时使用的字符序列,标识符由若干个字母.下划线_.和数字组成,且第一个字符必须是字母.通俗的讲就是凡可以自 ...
- spingboot中使用scheduled设置定时任务注意事项
在spring开发过程中经常会遇到需要定时执行的任务,如定时生成报表,定时推送消息等任务. springboot 提供了简单的 @Scheduled 表达式来配置定时任务.该方式默认是单线程的,任务在 ...
- php 键值数组搜索查询
php 键值数组查询 ,需要先将其转换为纯数组,然后才能用in_array 进行搜索. $arr_combos = [ ['id' => '1001', 'value' => 'zs' ...
- eclipse console 查看全部的输出
参考:https://blog.csdn.net/thatluck/article/details/52080736
- PHP开发环境(Apache+mysql+PHPstorm+php)的搭建
一.搭建思路 从浏览器到web服务器(Apache)到PHP环境到mysql数据库 二.环境搭建 1.浏览器(略) 2.Apache的安装与配置 1)官方下载地址:https://httpd.apac ...
- git 的那点东西,随心记
目前常用的项目版本管理,协同开发的工具有SVN和GIT,本次就记录一下GIT的基本使用. git下载地址:https://git-scm.com/downloads *根据自己的操作系统进行选择(这里 ...
- Ubuntu下安装 Mysql
MYSQL在ubuntu16.04下的编译安装mysql-5.6.23.tar.gz 为减少安装过程中因权限带来个各种问题,建议全程用root用户编译安装,步骤如下: 1.安装依赖文件 apt-ge ...
- <强化学习>基于采样迭代优化agent
前面介绍了三种采样求均值的算法 ——MC ——TD ——TD(lamda) 下面我们基于这几种方法来 迭代优化agent 传统的强化学习算法 || ν ν 已经知道完整MDP——使用价值函数V(s) ...
- 神奇的i++
神奇的i++ i++,++i,多简单啊,不需要深入研究吧!!! 我是这样想的. 直到我做了一道Java基础检测题,才发现,哦,原来是这样啊!!! 题是这样的 public class Demo { p ...
- 007、MySQL日期取当前时间,取昨天
#取今天文本格式 SELECT DATE_SUB( curdate( ), INTERVAL DAY ); #取昨天文本格式 SELECT DATE_SUB( curdate( ), INTERVAL ...