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/ 这个题目我感觉很难,看了半天别人写的答案,才明白个所以然.下面的代 ...
随机推荐
- hadoop tez 结合搭建以及测试异常解决
hadoop tez 搭建 1.下载tez,本人下载的是bin.0.92版本. http://www.apache.org/dyn/closer.lua/tez/0.9.2/ hadoop dfs - ...
- 20181115 python-第一章学习小结part3
第一章,基本数据类型-------仅学三种,字符型,数字型,布尔型 仅学三种数据类型: 字符型,加了引号的都可以被认为是字符串,字符串可以拼接 数字型,int,float,long三种,可以进行运算 ...
- 网页加水印 svg 方式
/** *网页加水印 svg 方式 * * @export * @param {*} [{ * container = document.body, * content = '请勿外传', * wid ...
- Vue(三十)公共组件
以 分页 组件为例:(根据自己具体业务编写) 1.pagination.vue <template> <!-- 分页 --> <div class="table ...
- 让数字变化炫酷起来,数字滚动Text组件[Unity]
让数字滚动起来 上周我的策划又提了样需求,当玩家评分发生变动时,屏幕出现人物评分浮层UI,播放评分数字滚动动画.这类数字滚动需求非常常见,我就按一般思路,将startvalue与endvalue每隔一 ...
- Java当中的IO三
1.处理流的使用实例 bufferedReader(处理流)介绍: buffereReader属于字符流,其次也是处理流,也是输入流,即字符输入处理流 常用方法 readLine() Buffered ...
- 10. vue axios 请求未完成时路由跳转报错问题
axios 请求未完成时路由跳转报错问题 前两天项目基本功能算是完成了,在公司测试时遇到了遇到了一个问题,那就是在请求未完成时进行路由跳转时会报错,想了几种办法来解决,例如加loading,请求拦截, ...
- Android中RadioGroup的初始化和简单的使用
一简介: RadioGroup作为一个单选按钮组,可以设置为性别选择男或则女,地址选择等等,作为一个android入门级选手,就简单的说一下RadioGroup组中RadioButton的布局和初始化 ...
- Android OpenGL ES 开发(七): OpenGL ES 响应触摸事件
像旋转三角形一样,通过预设程序来让对象移动对于吸引注意是很有用的,但是如果你想让你的OpenGL图形有用户交互呢?让你的OpenGL ES应用有触摸交互的关键是,扩展你的GLSurfaceView的实 ...
- JNI实战(一):JNI HelloWorld
使用最新Android Studio的Cmake,创建一个Native C++项目后,我们就可以看到JNI的Hello World的项目及示例代码了. JNI的项目代码,分为三层:Java层,C++层 ...