[leetcode]128. Longest Consecutive Sequence最长连续序列
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.
题目
给定一个数组,计算将其排序以后能形成的最长连续序列。
思路
如果允许O(nlogn)的复杂度,那么可以先排序。 以下是naive 的先排序的代码实现
class Solution {
public int longestConsecutive(int[] nums){
Arrays.sort(nums);
if(nums == null || nums.length == 0) return 0;
int result = 1;
int length = 1; for (int i = 1; i < nums.length; i++ ) {
if(nums[i] == nums[i-1] + 1 ){
length ++;
}else if (nums[i] == nums[i-1]){
continue;
}else{
length = 1; }
result = Math.max(result, length);
} return result;
}
}
再思考:
可是本题要求O(n)。
由于序列里的元素是无序的,又要求O(n),想到用哈希set。
代码
public int longestConsecutive(int[] nums) {
if(nums.length == 0) return 0;
Set<Integer> set = new HashSet<Integer>();
int max = 1;
for(int num : nums)
set.add(num);
for(int i = 0; i < nums.length; i++){
if(!(set.contains(nums[i] - 1))){
int currentSequence = 0;
int next = nums[i];
while(set.contains(next)){
currentSequence++;
max = Math.max(max, currentSequence);
next++;
}
}
} return max;
}
[leetcode]128. Longest Consecutive Sequence最长连续序列的更多相关文章
- 128. Longest Consecutive Sequence最长连续序列
[抄题]: Given an unsorted array of integers, find the length of the longest consecutive elements seque ...
- 298. Binary Tree Longest Consecutive Sequence最长连续序列
[抄题]: Given a binary tree, find the length of the longest consecutive sequence path. The path refers ...
- [Leetcode] Longest consecutive sequence 最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- [LeetCode] 128. Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- LeetCode 128 Longest Consecutive Sequence 一个无序整数数组中找到最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.Fo ...
- [LeetCode] 128. Longest Consecutive Sequence 解题思路
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- leetcode 128. Longest Consecutive Sequence ----- java
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- Java for LeetCode 128 Longest Consecutive Sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- Leetcode 128. Longest Consecutive Sequence (union find)
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Y ...
随机推荐
- C语言基础入门
搭建Windows平台C/C++开发环境 第1步: 第2步: 第3步: 第4步: 第5步: 第6步: 第7步: 第8步: 第9步: 第10步: 第11步: 第12步: 第13步: 第14步: 第15步 ...
- 跨域(五)——postMessage
HTML5的postMessage机制是客户端最直接的中档传输方法,一般用在iframe中父页与子页之间的客户端跨域通信. 浏览器支持情况:Chrome 2.0+.Internet Explorer ...
- ReactiveX 学习笔记(4)过滤数据流
Filtering Observables 本文主题为过滤 Observable 的操作符. 这里的 Observable 实质上是可观察的数据流. RxJava操作符(三)Filtering Deb ...
- intellij idea 配置web 项目
Intellij Idea 创建Web项目入门(一)(转载) 相关软件: Intellij Idea14:http://pan.baidu.com/s/1nu16VyD JDK7:http://p ...
- 处理TypeError: Converting circular structure to JSON
// Demo: Circular reference var o = {}; o.o = o; // Note: cache should not be re-used by repeated ca ...
- Android高级控件(下)
Chronometer计时器 常用的方法 getBase() 基准时间 setFormat 设置显示格式 start() 开始计时 stop() 停止计时 setOnChronometerListen ...
- canvas入门笔记
1.Canvas的使用注意 A.要在页面中添加一对canvas标记,默认占300*150的区域 B.我们可以通过html属性‘width’,‘height’来设置canvas的宽高,不可以通过cs ...
- winform中datagridview刷新后的排序记忆
datagridview先点标题排序,但是重新刷新之后,还是变成窗体加载后的样子 我这里用定时器刷新的. 1.先定义三个全局变量 /// <summary> /// 需要排序的列和方向 / ...
- javascript中 Array.prototype.slice的用法.
首先看到 www.w3school.cn上的解释:http://www.w3school.com.cn/jsref/jsref_slice_array.asp 定义和用法 slice() 方法可从已有 ...
- Java学习02 (第一遍)
巩固基础: byte 1个字节 -128到127 = 2^(字节数*8-1),转变2进制,01111111(负127)到11111111(正127) ,存在正负零(00000000),正零保留,负零补 ...