leetcode — longest-consecutive-sequence
import java.util.HashSet;
import java.util.Set;
/**
* Source : https://oj.leetcode.com/problems/longest-consecutive-sequence/
*
*
* Given an unsorted array of integers, find the length of the longest consecutive elements sequence.
*
* For example,
* Given [100, 4, 200, 1, 3, 2],
* The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4.
*
* Your algorithm should run in O(n) complexity.
*/
public class LongestConsecutiveSequence {
/**
* 求出数组中元素能构成连续序列的最大长度
*
* 题目中要求是O(n),所以不能使用排序的办法
* 针对每个元素arr[i]寻找数组中向前、向后有没有能构成连续序列的元素,查找的时候利用hash表
* 每次记录最大长度,如果找到了就把该元素从hash表中删除,因为该元素不会被再次查找到,所以从hash表中移除
* 如果hash表为空了,说明所有元素都被查找过,退出循环
*
* @param arr
* @return
*/
public int findLongestSequence (int[] arr) {
int max = 0;
Set<Integer> set = new HashSet<Integer>(arr.length);
for (int i = 0; i < arr.length; i++) {
set.add(arr[i]);
}
for (int i = 0; i < arr.length; i++) {
int curnum = arr[i] - 1;
int len = 1;
// 查找curnum左边的数字
while (set.contains(curnum)) {
set.remove(curnum);
curnum -= 1;
len++;
}
curnum = arr[i] + 1;
while (set.contains(curnum)) {
set.remove(curnum);
curnum += 1;
len++;
}
max = Math.max(max, len);
if (set.size() <= 0) {
return max;
}
}
return max;
}
public static void main(String[] args) {
LongestConsecutiveSequence longestConsecutiveSequence = new LongestConsecutiveSequence();
System.out.println(longestConsecutiveSequence.findLongestSequence(new int[]{100, 4, 200, 1, 3, 2}) + "----4");
}
}
leetcode — longest-consecutive-sequence的更多相关文章
- LeetCode——Longest Consecutive Sequence
LeetCode--Longest Consecutive Sequence Question Given an unsorted array of integers, find the length ...
- [LeetCode] Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- LeetCode: Longest Consecutive Sequence 解题报告
Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...
- [leetcode]Longest Consecutive Sequence @ Python
原题地址:https://oj.leetcode.com/problems/longest-consecutive-sequence/ 题意: Given an unsorted array of i ...
- [LeetCode] Longest Consecutive Sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- LeetCode: Longest Consecutive Sequence [128]
[题目] Given an unsorted array of integers, find the length of the longest consecutive elements sequen ...
- Leetcode: Longest Consecutive Sequence && Summary: Iterator用法以及ConcurrentModificationException错误说明
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- LeetCode—Longest Consecutive Sequence
题目描述: Given an unsorted array of integers, find the length of the longest consecutive elements seque ...
- [Leetcode] Longest consecutive sequence 最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- [Leetcode] Longest Consecutive Sequence 略详细 (Java)
题目参见这里 https://leetcode.com/problems/longest-consecutive-sequence/ 这个题目我感觉很难,看了半天别人写的答案,才明白个所以然.下面的代 ...
随机推荐
- 面试题int和Integer
int和Integer的区别 1.Integer是int的包装类,int则是java的一种基本数据类型 2.Integer变量必须实例化后才能使用,而int变量不需要 3.Integer实际是对象的引 ...
- :nth-child() 与 :nth-of-type(n)的区别
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 各个模块的刷新js
// 更新页面中的subgrid function refreshSubGrid(subgridName) { Xrm.Page.ui.controls.get(subgridName).refres ...
- MySQL5.6启用sha256_password插件
一.背景: 使用MySQL5.6过程中,发现默认的加密插件为mysql_native_password.而sha256_password的安全程度要比mysql_native_password高,尝试 ...
- 2.Git配置和关联GitHub
1.配置本地信息, 右键Git Bush Here git config –global user.name '账号名' ##回车 git config –global user.email 邮箱 # ...
- 4.23 Linux(3)
2019-4-23 19:03:53 买的服务器第三天感觉超爽!! 发现学习Linux超爽,有种操作的快感!!!!!是Windows比不了的!! 阿里巴巴镜像源 : https://opsx.alib ...
- 20175324 《Java程序设计》第七周学习总结
教材学习内容总结 常用实用类 String类 - 程序可以直接使用String类,但不能进行扩展,即String类不可以有子类 - 常用构造方法 - String(char a[])用一个字符数组a创 ...
- Java课程之团队开发(团队介绍)
一.介绍团队和团队成员 团队名称:凯域软创 团队成员介绍:张某某,崔某某,焦某某,陈某 二.关于团队作品 1.作品名称:课程表 2.你的创意解决了用户的什么需求:查看课程信息的需求 3.你有什么招数用 ...
- Linux Rabbit的使用
安装RabbitMQ 1.安装Erlang yum -y install epel-release yum -y update yum -y install erlang socat yum -y i ...
- FFmpeg 结构体学习(六): AVCodecContext 分析
在上文FFmpeg 结构体学习(五): AVCodec 分析我们学习了AVCodec结构体的相关内容.本文,我们将讲述一下AVCodecContext. AVCodecContext是包含变量较多的结 ...