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 ...
随机推荐
- Firewalld--02 端口访问/转发、服务访问、源地址管理
目录 防火墙端口访问/转发.服务访问.源地址管理 1. 防火墙端口访问策略 2. 防火墙服务访问策略 3.防火墙接口管理 4.防火墙源地址管理 5. 防火墙端口转发策略 防火墙端口访问/转发.服务访问 ...
- 获取服务进程server.exe的pid(0号崩溃)
#include "stdafx.h" #include <windows.h> #include <iostream> #include <COMD ...
- Ansible笔记(2)---常用模块之文件操作
一.copy模块 1.1作用: copy模块是将ansible主机上的文件拷贝到远程受控主机 1.2常用参数: src参数 :用于指定需要copy的文件或目录. dest参数 :用于指定文件将被拷贝到 ...
- 6.dockerfile
一.概述 自制镜像的目的不是为了解决配置更新的问题,而是为了定制化应用服务. 镜像的制作:基于容器制作:dockerfile dockerfile的格式:注释信息+指令(约定俗成使用大写)及其参数 d ...
- python类对象属性查找原理
class Foo(object): def __init__(self): # 这是一个对象属性 self.obj_pro = 12 # 这是一类属性 c_pro = 11 # 这是一个静态方法 @ ...
- python学习笔记(十三)处理时间模块
import time time.sleep(2)#等待几秒 时间的三种表现方式: 1.格式化好的时间 2018-1-14 16:12 2.时间戳 是从unix元年到现在所有的秒数 3.时间元组 想时 ...
- Jenkins修改升级配置
更换升级配置如下: http://mirror.esuni.jp/jenkins/updates/update-center.json
- 树上倍增 x
树上倍增. dfs序的做法: 思路: //f[i][j]表示dfs序中序号为i到序号为j的点之间深度最小的点的编号 dfs序[]//存0-...(id)编号 节点[]//存dfs序中所经过的节点号 d ...
- 阿里云数据库导出-本地mysql导入
因阿里云数据库标准访问模式不支持外网接入 因此导出一份到本地,注意选择编码为utf8 mysql 命令行用source导入有utf8编码的sql文件时导入的数据有乱码解决办法 set names ut ...
- PHP读取XML文件数据获取节点值
最近在接入渠道的时候遇到接口返回是xml数据.现在接口数据返回json数据格式比较常见. 如何获取xml里面真正数据? 对象结果集合单个值的强制转换处理.(直接代码说明) demo示例: 创建xml ...