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 ...
随机推荐
- Flink学习(七) 多流转换算子 拆分合并流
一.Split 和 Select (使用split切分过的流是不能被二次切分的) DataStream --> SplitStream : 根据特征把一个DataSteam 拆分成两个或者多个D ...
- 面试题58 - I. 翻转单词顺序
地址:https://leetcode-cn.com/problems/fan-zhuan-dan-ci-shun-xu-lcof/ <?php/**输入一个英文句子,翻转句子中单词的顺序,但单 ...
- MyBatis与其使用方法讲解
ORM 在讲解Mybatis之前,我们需了解一个概念ORM(Object-Relational Mapping)对象关系映射,其是数据库与Java对象进行映射的一个技术.通过使用ORM,我们可以不用编 ...
- AtCoder Beginner Contest 396-e
原题链接 思路 看到这道题,很明显就能发现这道题其实跟图论有关,将\(A\)数组看成一张无向图,每一个节点\(i\)的点权就是\(A_i\),每两个节点\(i\)和\(j\)之间的边权就是\(A_i ...
- Golang 1.16新特性-embed包及其使用
embed 是什么 embed是在Go 1.16中新加入的包.它通过//go:embed指令,可以在编译阶段将静态资源文件打包进编译好的程序中,并提供访问这些文件的能力. 为什么需要 embed 包 ...
- 怎么解决DB读写分离,导致数据不一致问题?
前言 在互联网中大型项目中,读写分离应该是我们小伙伴经常听说的,这个主要解决大流量请求时,提高系统的吞吐量.因为绝大部分互联网产品都是读多写少,大部分都是读请求,很小部分是写请求. 上图: 1)一个主 ...
- 设置git忽略文件
要设置Git忽略文件,你可以使用一个名为.gitignore的特殊文件.在这个文件中,你可以列出需要Git忽略的文件.文件夹.或者匹配模式.当Git执行操作时,它会自动忽略这些被列出的文件. 1. 在 ...
- 继承内存图--java进阶 day01
主方法进栈,有new进堆 堆内存中先存自己类中有的变量 又因为继承了父类,所以父类中的变量也要存入 即使被私有化,依旧可以继承,只是没有权限使用! 创建对象时,会调用构造方法,所以走构造方法,实参传形 ...
- 【Java】网络编程
InternetAccess类的使用 一.概述 计算机网络: 把分布在不同地理区域的计算机与专门的外部设备用通信线路互连成一个规模大.功能强的网络系统,从而使众多的计算机可以方便地互相传递信息共享硬件 ...
- 如何确定dbgrid选择的是记录而不是分组
with cxgrdbtblvwGrid1DBTableView1.Controller do if FocusedRecord is TcxGridDataRow then begin i := c ...