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 ...
随机推荐
- LCP 17. 速算机器人
地址:https://leetcode-cn.com/problems/nGK0Fy/ <?php /** LCP 17. 速算机器人 小扣在秋日市集发现了一款速算机器人.店家对机器人说出两个数 ...
- 解密prompt系列50. RL用于优化Agent行为路径的一些思路
OpenAI新推出的Deep Research功能,属实有些惊艳,也验证了去年的一些观点,之后的大模型工作流会呈现一些截然不同的形态,有敏捷型的例如语音端到端的及时对话,也会有异步长流程的复杂任务,去 ...
- 记录一次关于使用leaflet draw 插件叠加图层删除绘制层无法删除的问题
问题描述 业务逻辑是这样的:再地图上已经绘制了一个多边形区域,然后需要再绘制的区域下再绘制下级区域,使用插件可以正常绘制并保存绘制数据,然后再回显编辑的时候,此时地图展示了上级多边形区域(该区域未追加 ...
- 记一次QT的QSS多个控件设置同一个样式的问题
文章目录 Qt样式表的格式问题 问题的引入 qss 选择器 问题所在 Reference Qt样式表的格式问题 问题的引入 最近在进行样式设计的时候,发现了一个问题,具体如下: 我是将所有样式写到.q ...
- PV、UV、VV、IP含义及计算方式
什么是PV? PV 即 Page View,网站浏览量,指页面浏览的次数,用以衡量网站用户访问的网页数量. 用户每次打开一个页面便记录1次PV,多次打开同一页面则浏览量累计.一般来说,PV与来访者的数 ...
- maven为什么发生依赖冲突?怎么解决依赖冲突?
maven为什么发生依赖冲突?怎么解决依赖冲突? 我们在开发的时候,偶尔会遇到依赖冲突的时候,一般都是NoClassDefFoundError.ClassNotFoundException.NoSuc ...
- K8S 问题排查: cgroup 内存泄露问题
Posted on 2019年12月6日Leave a comment Contents [hide] 1 前言 2 现象 3 原因 4 解决方案 4.1 方案一 4.2 方案二 4.3 方案三 5 ...
- udl(Universal Data Link)通用数据连接文件
新建文本文档 更改后缀名为.udl(注意小点) 然后打开运行 配置并测试 改回后缀名.txt(有个小点哦) 打开就是了
- IP地址查询服务
IP地址查询站点 https://ip.cn/ http://ip.qq.com/ http://ip138.com/ https://www.apnic.net/ ... IP计算 ip地址在线计算 ...
- 使用Python解决氢原子问题
引言 大家好!今天我们将讨论一个非常经典的物理问题-氢原子问题,并使用 Python 来进行求解.氢原子问题是量子力学中的基础问题,它帮助我们理解原子内部的电子结构及其能量水平.通过这篇文章,大家将学 ...