[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 ...
随机推荐
- Servlet基本_初期化パラメータ、外部ファイル
1.サーブレットの初期化パラメータサーブレットの初期化パラメータを利用するには.必ずweb.xmlにおいてサーブレットマッピングを指定する必要がある.(Tomactのinvokerサーブレットは利用で ...
- ReactiveX 学习笔记(5)合并数据流
Combining Observables 本文的主题为合并 Observable 的操作符. 这里的 Observable 实质上是可观察的数据流. RxJava操作符(四)Combining An ...
- 记录git的初始设置,添加文件,提交文件
1 初始配置 git config --global user.name "" //配置用户名 git config --global user.email "&quo ...
- Statemnet和PerparedStstemnent有哪些区别
Statement 和 PreparedStatement之间的关系和区别. 关系:PreparedStatement继承自Statement,都是接口 区别:PreparedStat ...
- 28.注解2.md
目录 1. 特点 2.优点 3. 源注解-部分 4.自定义注解 5.使用注解获 1. 特点 注释:给程序员阅读使用 注解:给编译器阅读使用 2.优点 简化配置文件 灵活方便 3. 源注解-部分 //修 ...
- you-get
1.打开cmd,输入命令并执行 pip3 install you-get 2.输入命令,检测 You-Get 是否安装成功 you-get 3.开始下载吧 you-get [视频地址]you-get ...
- 学JS的心路历程-函式(二)arguments
参数(argument)与函式参数(parameter) 在讨论函式时,很多人都会把这两个搞混,我自己也不例外. 虽然讲错别人也听得懂,但是我们还是要搞清楚这两个的定义到底是什么! 参数是当我们呼叫函 ...
- vue组件知识点
1.组件的定义 const component = { props: { //外部父组件约束子组件的 里面不要修改 可以通过触发事件来修改 active: Boolean, propOne: Stri ...
- yii gii配置ip限制使用gii
<?php $config = [ 'components' => [ 'request' => [ // !!! insert a secret key in the follow ...
- multi role
mesos 1.2.0实验性的支持了一个框架多个role. message FrameworkInfo { ... // Roles are the entities to which allocat ...