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 ...
随机推荐
- MYSQL学习笔记——数据库范式及MYSQL优化整体思路
一.数据库范式 为了建立冗余较小.结构合理的 ...
- 23飞机大战__pygame 快速入门
1. 使用 pygame 创建图形窗口 小节目标 游戏的初始化和退出 理解游戏中的坐标系 创建游戏主窗口 简单的游戏循环 可以将图片素材 绘制 到 游戏的窗口 上, 开发游戏之前需要先知道 如何建 ...
- springboot 集成rabbitMQ
package com.jd.ng.shiro.config.rabbitMQconfig; import com.jd.ng.shiro.rabbitMqListener.SimpleMessage ...
- Python操作cx_Oracle笔记
参考文章: http://cx-oracle.readthedocs.io/en/latest/cursor.html # 创建数据库连接 ordb = Oracle.connect("{0 ...
- java源码生成可运行jar
参考资料:https://blog.csdn.net/whatday/article/details/54767187 源码目录层级如下:
- [luogu] P3809 【模板】后缀排序 (SA)
板子,照着题解打的倍增版. #include <iostream> #include <cstdio> #include <cstring> using names ...
- [CSP-S模拟测试]:棋盘(数学+高精度)
题目描述 在一个大小为$N\times N$的棋盘上,放置了$N$个黑色的棋子.并且,对于棋盘的每一行和每一列,有且只有一个棋子.现在,你的任务是再往棋盘上放置$N$个白色的棋子.显然,白色棋子不能与 ...
- 五一清北学堂培训之数据结构(Day 1&Day 2)
Day 1 前置知识: 二进制 2.基本语法 3.时间复杂度 正片 1.枚举 洛谷P1046陶陶摘苹果 入门题没什么好说的 判断素数:从2枚举到sqrt(n),若出现n的因子,则n是合数 ...
- 仅对原表新增列的全量数据.csv
w
- zookeeper分布式锁用法
package com.example.demo3.zk; import lombok.extern.slf4j.Slf4j; import org.apache.storm.shade.org.ap ...