【leetcode刷题笔记】Longest Consecutive Sequence
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.
题解:首先构建一个hashmap,键值为num中的所有元素,这样查找一个元素是否在num中就只需要O(1)的时间了。
接下来以上面给定的例子来说明算法:
比如上述num数组中第一个元素是100,那么就找101或者99在不在num中,发现不在;
num中第二个元素是4,那么就找3或者5是否在num中,发现3在num中,而5不在;于是继续找2在不在num中,然后找1在不在num中.....最终知道有4,3,2,1这么一个长度为4的序列。在这个遍历过程中,因为已经找到了序列4,3,2,1,所以如果以后遍历到3的时候再找到的序列3,2,1已经没有意义了(肯定比当前的最长序列短),所以3,2,1这三个元素在未来的遍历中,我们都不需要考虑了,这里就把它们在hashmap中对应的值改成1,作为标志。
num中第三个元素是200,那么就找199或者201是否在num中,发现都不在;
num中还有元素1,3,2,基于上述描述的原因,就不再遍历了。
这样就保证了每个元素最多遍历一次,时间复杂度为O(n)。
示例图如下;

代码如下:
public class Solution {
public int longestConsecutive(int[] num) {
int answer = 0;
HashMap<Integer, Integer> map = new HashMap<Integer,Integer>();
for(int i:num)
map.put(i, 0);
for(int i:num){
if(map.get(i) == 1)
continue;
int currMax = 1;
int tmp = i-1;
//left
while(map.containsKey(tmp)){
map.put(tmp, 1); //we have visited tmp,so we don't start with tmp in the future
currMax++;
tmp--;
}
tmp = i+1;
while(map.containsKey(tmp)){
map.put(tmp, 1);
currMax++;
tmp++;
}
if(currMax > answer)
answer = currMax;
}
return answer;
}
}
【leetcode刷题笔记】Longest Consecutive Sequence的更多相关文章
- 【leetcode刷题笔记】Permutation Sequence
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [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] 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
原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence-ii/description/ 题目: G ...
- LeetCode 298. Binary Tree Longest Consecutive Sequence
原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/ 题目: Given a binary t ...
- LeetCode刷题笔记和想法(C++)
主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...
- 算法题丨Longest Consecutive Sequence
描述 Given an unsorted array of integers, find the length of the longest consecutive elements sequence ...
- [LeetCode] 549. Binary Tree Longest Consecutive Sequence II_ Medium tag: DFS recursive
Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree. Especia ...
- 18.9.10 LeetCode刷题笔记
本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...
随机推荐
- thinkPHP5.0的学习研究【序言】
2017年6月19日13:19:151.ThinkPHP V5.0——为API开发而设计的高性能框架2.ThinkPHP是一个免费开源的,快速.简单的面向对象的轻量级PHP开发框架,是为了敏捷WEB应 ...
- Windows 下Node.js开发环境配置
第一步:安装VirtualBox(以管理员身份安装) 1.安装完成后,打开VirtualBox,点击“新建”按钮,输入信息,“下一步”(名称可任意) 2.设置内存为1024MB,“下一步”—>“ ...
- task1-9
今天完成: Task1.参考修真院线下报名贴(学习资料-线下报名-北京报名)中报名的格式,整理出业务模型,确定需要几个对象,每个对象的属性是什么,对象和对象之间的关系是一对一,还是一对多. [参考资料 ...
- jvm本身的多线程机制
1 多线程环境下的构造函数调用 构造函数本身并没有隐式的同步,因为各个线程构建的是自己的对象,它们之间是不存在竞争关系的. 2 class loader在load class时被了sychronize ...
- centos 安装composer PHP项目部署,Composer install Do not run Composer as root/super user!
使用composer 安装项目的时候遇到了 Composer install Do not run Composer as root/super user! 在博客https://segmentfau ...
- ALV 表头 ADD_TEXT
[转自http://lz357502668.blog.163.com/blog/static/16496743201252891452493/] CALL METHOD valid_reference ...
- ssm框架与shiro的整合小demo,用idea开发+maven管理
shiro安全框架是目前为止作为登录注册最常用的框架,因为它十分的强大简单,提供了认证.授权.加密和会话管理等功能 . shiro能做什么? 认证:验证用户的身份 授权:对用户执行访问控制:判断用户是 ...
- Linux基础系列:常用命令(6)_nfs服务与nginx服务
NFS介绍: NFS 是Network File System的缩写,即网络文件系统.一种使用于分散式文件系统的协定,由Sun公司开发,于1984年向外公布.功能是通过网络让不同的机器.不同的操作系统 ...
- LeetCode:移除K位数字【402】
LeetCode:移除K位数字[402] 题目描述 给定一个以字符串表示的非负整数 num,移除这个数中的 k 位数字,使得剩下的数字最小. 注意: num 的长度小于 10002 且 ≥ k. nu ...
- Windows命令行(DOS命令)教程
一.命令行简介 命令行就是在Windows操作系统中打开DOS窗口,以字符串的形式执行Windows管理程序. 在这里,先解释什么是DOS? DOS——Disk Operation System 磁盘 ...