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/ 这个题目我感觉很难,看了半天别人写的答案,才明白个所以然.下面的代 ...
随机推荐
- 练习2-1 Programming in C is fun!
练习2-1 Programming in C is fun! 一 问题描述 本题要求编写程序,输出一个短句“Programming in C is fun!”. 输入格式: 本题目没有输入. 输出格式 ...
- BZOJ5335 : [TJOI2018]智力竞赛
二分答案,转化成求最少的路径,覆盖住所有权值$\leq mid$的点. 建立二分图,若$i$的后继为$j$,则连边$i\rightarrow j$,求出最大匹配,则点数减去最大匹配数即为最少需要的路径 ...
- teamviewer quicksupport 插件(下载)
teamviewer quicksupport 插件(下载) teamviewer是一款远程控制软件(免费,比较好的); teamviewer quicksupport是一款支持手机可以被远程控制软件 ...
- myeclipse中的HTML页面在浏览器中显示为乱码
myeclipse中的HTML页面在浏览器中显示为乱码 在通过myeclipse开发项目的过程中,如果用HTML页面书写前端,可能出现中文乱码现象,需要怎么解决呢?下面是我从网上搜的方法: 解决办法: ...
- 获取subgrid中的数据并修改,含添加刷新列表的事件
var isAddRefresh = false; function setLawsuitQueryResultText() { var queryResultIndex = 7; var gridC ...
- jenkins的sbt插件安装
在jenkins服务器手动安装sbt curl https://bintray.com/sbt/rpm/rpm > bintray-sbt-rpm.repo mv bintray-sbt-rpm ...
- Java GC相关知识
Java堆的分类 分为两类:YoungGen和OldGen.其中,YoungGen分为三部分:eden,from survivor和to survivor,比例默认是:8:1:1 PermGen不属于 ...
- PageHelper分页插件的使用
大家好!今天写ssm项目实现分页的时候用到pageHelper分页插件,在使用过程中出现了一些错误,因此写篇随笔记录下整个过程 1.背景:在项目的开发的过程中,为了实现所有的功能. 2.目标:实现分页 ...
- Vs 发布编译问题
如果勾选了预编译 发布后的目录会有PrecompiledApp.config文件,bin目录中会有App_global.asax.dll和App_global.asax.compiled文件 不勾选预 ...
- [Swift]LeetCode675. 为高尔夫比赛砍树 | Cut Off Trees for Golf Event
You are asked to cut off trees in a forest for a golf event. The forest is represented as a non-nega ...