【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 天的价格. ...
随机推荐
- Linux 安装OpenSSL出错的解决方法
以前编译php没有 –with–openssl 现在要使用到 openssl ,phpinze扩展安装,但是在make时候报错 今天找这个在网上找了大半天,最后总结应该是php版本本身的问题,错误是p ...
- H - Funny Car Racing
H - Funny Car Racing Time Limit:1000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Desc ...
- [原创]关于absolute、relative和float的一些思考
absolute: 元素完全脱离文档流,不占文档流的位置,不使用top.left等属性时,仍然在原文档流位置上(但是不在文档流中,也不占用位置),设置了top.left等之后,向上寻找到第一个非sta ...
- sgu 195 New Year Bonus Grant【简单贪心】
链接: http://acm.sgu.ru/problem.php?contest=0&problem=195 http://acm.hust.edu.cn/vjudge/contest/vi ...
- nginx服务器的内核调优
TCP公有类 net.core.somaxconn = 262144 net.core.netdev_max_backlog = 262144 net.ipv4.ip_local_port_range ...
- Notepad++ Tidy2 插件的核心配置
在已有配置的基础上加上这四行: 以免符号被转换成HTML实体了 preserve-entities: yes quote-ampersand: yes quote-marks: no quote-nb ...
- ASP连接数据库SQLServer
Set conn=Server.CreateObject("adodb.connection")Set conn1=Server.CreateObject("adodb. ...
- Webpack探索【5】--- plugins详解
本文主要讲plugins相关内容. https://gitbook.cn/gitchat/column/59e065f64f7fbe555e479204/topic/59e96d87a35cf44e1 ...
- springboot带分页的条件查询
QueryDSL简介 QueryDSL仅仅是一个通用的查询框架,专注于通过Java API构建类型安全的SQL查询. Querydsl可以通过一组通用的查询API为用户构建出适合不同类型ORM框架或者 ...
- Android系统移植与调试之------->如何修改Android设备的桌面背景图片
1.切换到~/mx0831-0525/device/other/TBDG1073/overlay/frameworks/base/core/res/res目录 2.准备好一张相应尺寸的图片并且命名为d ...