76.Longest Consecutive Sequence(最长的连续序列)
Level:
Hard
题目描述:
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.
思路分析:
设置一个hashset,保存数组中出现的值,然后遍历hashset,每访问到一个值num,我们查看num-1,和num+1是否在集合中,如果存在那我们可以继续查找num-2,和num+2是否存在,我们按照这种方法查找,直到要查找的数不存在,可以计算出一个连续的序列长度,对集合中每个元素进行上述操作,最后得出一个最长的连续序列。
代码:
public class Solution{
public int longestConsecutive(int []nums){
if(nums==null||nums.length==0)
return 0;
HashSet<Integer>set=new HashSet<>();
int res=0; //记录结果
for(int n:nums)
set.add(n);
while(!set.isEmpty()){
int num=getfirst(set);
set.remove(num);
int left=0;//记录当前num向左能延伸的距离
while(set.contains(num-1)){
left++;
set.remove(num-1);
num--;
}
int right=0; //记录当前num向右能延伸的距离
num=num+left;
while(set.contains(num+1)){
right++;
set.remove(num+1);
num++;
}
res=Math.max(res,left+right+1);
}
return res;
}
public int getfirst(HashSet<Integer>set){//返回集合中第一个元素
for(int num:set)
return num;
return 0;
}
}
76.Longest Consecutive Sequence(最长的连续序列)的更多相关文章
- [Leetcode] Longest consecutive sequence 最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- 298. Binary Tree Longest Consecutive Sequence最长连续序列
[抄题]: Given a binary tree, find the length of the longest consecutive sequence path. The path refers ...
- [leetcode]128. Longest Consecutive Sequence最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Y ...
- 128. Longest Consecutive Sequence最长连续序列
[抄题]: Given an unsorted array of integers, find the length of the longest consecutive elements seque ...
- [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 II 二叉树最长连续序列之二
Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree. Especia ...
- [Swift]LeetCode298. 二叉树最长连续序列 $ Binary Tree Longest Consecutive Sequence
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- [LeetCode] 128. Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
随机推荐
- SSM架构 (Spring 5.0.2)添加Jackson
第一步添加jsckson的包 <dependency> <groupId>javax.annotation</groupId> <artifactId> ...
- ltp-ddt eth_switch_config学习
# @name ALE Table test using SWITCH-CONFIG # @desc Checks default entries in ALE table and verifies ...
- CSS3选择器 ::before和::after
:before和:after伪元素的用法 :before和:after伪元素的用法十分简单:下面的代码样例中, :before 和 :after 将会在 blockquote 元素之前和之后插入新内容 ...
- BZOJ 3729 GTY的游戏
伪ETT? 貌似就是Splay维护dfn = = 我们首先观察这个博弈 这个博弈直接%(l+1)应该还是很显然的 因为先手怎么操作后手一定能保证操作总数取到(l+1) 于是就变成阶梯Nim了 因为对于 ...
- YOLOV3算法详解
YOLOV3 YOLO3主要的改进有:调整了网络结构:利用多尺度特征进行对象检测:对象分类用Logistic取代了softmax. 新的网络结构Darknet -53 darknet-53借用了re ...
- sed进阶
下面这些命令未必经常会用到,但当需要时,知道这些肯定是件好事. 一.多行命令 sed命令通常是对一行数据进行处理,然后下一行重复处理. sed编辑器包含了三个可用来处理多行文本的特殊命令 N:将数据流 ...
- 检测代理IP匿名程度的方法,很实用
做网络的基本都知道代理,这个是肯定的,不管是用花刺还是猎手的网页代理,还是直接VPN的通道代理,代理有着不用说大家也知道的重要性.不管是做CPA还是做点击亦或者投票,代理都能帮我们一下,虽然帮的忙不大 ...
- [hadoop](2) MapReducer:Distributed Cache
前言 本章主要内容是讲述hadoop的分布式缓存的使用,通过分布式缓存可以将一些需要共享的数据在各个集群中共享. 准备工作 数据集:ufo-60000条记录,这个数据集有一系列包含下列字段的UFO目击 ...
- [CSP-S模拟测试]:chess(搜索+最短路)
题目描述 $pig$在下象棋的时候特别喜欢用马,他总是计算着自己的马还需要几步才能吃掉对方的帅,以及方案数的个数,当然$pig$很笨,所以他只能求助于你.我们假设在$n\times m$的棋盘上,$p ...
- php 中 http_build_query用法
http_build_query (PHP 5) http_build_query -- 生成 url-encoded 之后的请求字符串描述string http_build_query ( arra ...