Longest Consecutive Sequence——LeetCode进阶路
原题链接https://leetcode.com/problems/longest-consecutive-sequence/
题目描述
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.
Your algorithm should run in O(n) complexity.
Example:
Input: [100, 4, 200, 1, 3, 2]
Output: 4
Explanation: The longest consecutive elements sequence is [1, 2, 3, 4]. Therefore its length is 4.
Accepted
192,857
Submissions
470,536
思路分析
利用set或者Hashmap都OK
!!!一定记得会有重复元素的情况出现,别问我怎么知道的……
源码附录
class Solution {
public int longestConsecutive(int[] nums) {
if(nums == null || nums.length == 0)
{
return 0;
}
int result = 0;
HashMap<Integer,Integer> map = new HashMap<>();
for(int i:nums)
{
if(map.getOrDefault(i,0) == 0)//记得排除元素重复访问情况!……卡了好久
{
int low = map.getOrDefault(i-1,0);
int high = map.getOrDefault(i+1,0);
map.put(i,low+1+high);
if(low >= 1)
{
map.put(i-low,low+1+high);
}
if(high >= 1)
{
map.put(high+i,low+1+high);
}
result = Math.max(result,low+1+high);
}
}
return result;
}
}
Longest Consecutive Sequence——LeetCode进阶路的更多相关文章
- 128. Longest Consecutive Sequence(leetcode)
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- Binary Tree Longest Consecutive Sequence -- LeetCode
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- Longest Consecutive Sequence [LeetCode]
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- Longest Consecutive Sequence——Leetcode
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- Longest Consecutive Sequence leetcode java
题目: Given an unsorted array of integers, find the length of the longest consecutive elements sequenc ...
- [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
原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/ 题目: Given a binary t ...
- 【LeetCode OJ】Longest Consecutive Sequence
Problem Link: http://oj.leetcode.com/problems/longest-consecutive-sequence/ This problem is a classi ...
- [LeetCode] 128. Longest Consecutive Sequence 解题思路
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
随机推荐
- 用Logseq记日报和管理文献
优缺点浅评 Logseq是一款双链笔记软件,其优点结合使用场景概括来说包括 开箱即用的极简界面,非常适合用来写日报 灵活的双链,强大的PDF标注,适合构建文献库 使用markdown格式来本地存储笔记 ...
- maven - [02] settings.xml配置
maven处理配置的优先级顺序 (1)全局settings.xml(优先级★☆☆☆☆) 位于Maven安装目录的conf/settings.xml,提供系统级的默认配置,比如本地仓库位置.远程仓库列表 ...
- Linux - 基础环境检查
检查操作系统:建议根据实际产品需要进行安装 检查主机名:集群中统一前缀并区分服务器功能,小写命名 检查内存:建议至少128G 检查CPU:建议至少2个支持超线程技术的10核芯片 检查磁盘:同一功能的服 ...
- 【BUUCTF】Easy Java
[BUUCTF]Easy Java 题目来源 收录于:BUUCTF RoarCTF 2019 题目描述 经典登录框 不过SQL注入.目录扫描都没有发现 题解 点击页面的 help 跳转到/Downlo ...
- 读论文-协同过滤技术综述(A Survey of Collaborative Filtering Techniques)
前言 今天读的一篇论文题目为<协同过滤技术综述>(A Survey of Collaborative Filtering Techniques),文章发表于<人工智能研究进展> ...
- 详解nginx配置url重定向-反向代理
https://www.jb51.net/article/99996.htm 本文系统:Centos6.5_x64 三台主机:nginx主机,hostname: master.lansgg.com ...
- Golang 1.16新特性-embed包及其使用
embed 是什么 embed是在Go 1.16中新加入的包.它通过//go:embed指令,可以在编译阶段将静态资源文件打包进编译好的程序中,并提供访问这些文件的能力. 为什么需要 embed 包 ...
- DB读写分离情况下,如何解决缓存和数据库不一致性问题?
前言 在读写分离的情况下,缓存和数据库数据不一致怎么解决? 请看这一篇如何更新缓存保证缓存和数据库双写一致性? 如何解决DB数据库的数据不一致问题? 请看这一篇怎么解决DB读写分离,导致数据不一致问题 ...
- MySQL-InnoDB行锁
InnoDB的锁类型 InnoDB存储引擎支持行锁,锁类型有两种: 共享锁(S锁) 排他锁(X锁) S和S不互斥,其他均互斥. 除了这两种锁以外,innodb还支持一种锁,叫做意向锁. 那么什么是意向 ...
- 模板设计模式--java进阶day03
1.模板设计模式 说到模板,我们第一时间想到的可能就是写作文 不过这样写就是一篇完整的作文,我们应该进行修改 这样修改还会存在一个问题,每一个人写的作文不同,中间的body()无法描述清楚,所以我们要 ...