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 ...
随机推荐
- 面试题55 - II. 平衡二叉树
地址:https://leetcode-cn.com/problems/ping-heng-er-cha-shu-lcof/ <?php /** 输入一棵二叉树的根节点,判断该树是不是平衡二叉树 ...
- AI与.NET系列文章之三:在.NET中使用大语言模型(LLMs)
引言 在技术迅猛发展的今天,大语言模型(Large Language Models, LLMs)已成为人工智能领域的核心驱动力之一.从智能对话系统到自动化内容生成,LLMs的应用正在深刻改变我们的工作 ...
- 【由技及道】量子构建交响曲:Jenkinsfile流水线的十一维编程艺术【人工智障AI2077的开发日志008】
摘要:当代码提交触发时空涟漪,当构建流水线穿越量子维度--欢迎来到自动化构建的终极形态.本文将揭示如何用Jenkinsfile编写量子构建乐章,让每次代码提交都成为跨维度交响乐的音符. 动机:构建系统 ...
- 【数值计算方法】蒙特卡洛方法积分的Python实现
原理不做赘述,参见[数值计算方法]数值积分&微分-python实现 - FE-有限元鹰 - 博客园,直接上代码,只实现1d,2d积分,N维积分的蒙特卡洛方法也类似. 代码 from typin ...
- 【数学公式】mathtype和word2016集成
mathtype 安装好了以后,word 没有相应的选项卡怎么办? 问题 解决办法 找到word的启动路径 2. 找到mathtype 安装好后的mathpage文件夹 进入文件夹,找到MathPag ...
- 基于近红外与可见光双目摄像头的人脸识别与活体检测,文末附Demo
基于近红外与可见光双目摄像头的活体人脸检测原理 人脸活体检测(Face Anti-Spoofing)是人脸识别系统中的重要一环,它负责验证捕捉到的人脸是否为真实活体,以抵御各种伪造攻击,如彩色纸张打印 ...
- css3 渐变边框如何实现圆角效果
常规的 border-image 属性如果直接使用 border-radius 会无效,关于如何实现渐变边框圆角,网上流传着大概这么几种办法: 渐变背景方式(仅适用于纯底色背景) 借助 after 伪 ...
- PaddleOCR学习笔记3-通用识别服务
今天优化了下之前的初步识别服务的python代码和html代码. 采用flask + paddleocr+ bootstrap快速搭建OCR识别服务. 代码结构如下: 模板页面代码文件如下: uplo ...
- IvorySQL 4.4 发布
IvorySQL 4.4 已于 2025 年 3 月 10 日正式发布.新版本全面支持 PostgreSQL 17.4,新增多项新功能,并修复了已知问题. 增强功能 PostgreSQL 17.3 增 ...
- 在 ThinkPHP 6 控制器中使用文件锁机制
创建锁管理类 首先,创建一个锁管理类来处理文件锁: namespace app\common\service; use Exception; class LockManager { private $ ...