leetcode 128. Longest Consecutive Sequence ----- java
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.
就是判断数组中最长的连续数字的长度。
1、直接用排序。。当然不行了,时间复杂度超过了O(n),虽然AC了。
public class Solution {
public int longestConsecutive(int[] nums) {
int result = 1;
if( nums.length == 0)
return result;
Arrays.sort(nums);
for( int i = 1;i<nums.length;i++){
int a = 1;
while( i<nums.length && (nums[i] == nums[i-1]+1 || nums[i] == nums[i-1]) ){
if( nums[i] == nums[i-1]+1 )
a++;
i++;
}
result = Math.max(a,result);
}
return result;
}
}
2、用set集合。遍历两次,得出结果,由于add,remove,contains都是O(1)的复杂度,所以时间复杂度符合题意。
public class Solution {
public int longestConsecutive(int[] nums) {
if( nums.length == 0)
return 0;
Set set = new HashSet<Integer>();
for( int num : nums)
set.add(num);
int result = 1;
for( int e : nums){
int left = e-1;
int right = e+1;
int count = 1;
while( set.contains(left )){
set.remove(left);
count++;
left--;
}
while( set.contains(right) ){
set.remove(right);
count++;
right++;
}
result = Math.max(result,count);
}
return result;
}
}
leetcode 128. Longest Consecutive Sequence ----- java的更多相关文章
- [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. 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最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Y ...
- Leetcode 128. Longest Consecutive Sequence (union find)
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Y ...
- 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
原题地址 1. 把所有元素都塞到集合里2. 遍历所有元素,对于每个元素,如果集合里没有,就算了,如果有的话,就向左向右拓展,找到最长的连续范围,同时在每次找的时候都把找到的删掉.这样做保证了同样的连续 ...
- 128. Longest Consecutive Sequence(leetcode)
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- 【LeetCode】128. Longest Consecutive Sequence
Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...
随机推荐
- Deep Learning In NLP 神经网络与词向量
0. 词向量是什么 自然语言理解的问题要转化为机器学习的问题,第一步肯定是要找一种方法把这些符号数学化. NLP 中最直观,也是到目前为止最常用的词表示方法是 One-hot Representati ...
- K2与OData和Swagger集成,从任何设备无需代码获取数据
K2近期宣布获得了DData和Swagger REST的支持,这件事情究竟有多好呢? K2与OData和Swagger的集成,保障K2 Blackpearl的用户能建立基于工作流和表单的解决方案,最重 ...
- iOS 高效添加圆角效果实战讲解
圆角(RounderCorner)是一种很常见的视图效果,相比于直角,它更加柔和优美,易于接受.但很多人并不清楚如何设置圆角的正确方式和原理.设置圆角会带来一定的性能损耗,如何提高性能是另一个需要重点 ...
- swift语言之多线程操作和操作队列(下)———坚持51天吃掉大象(写技术文章)
欢迎有兴趣的朋友,参与我的美女同事发起的活动<51天吃掉大象>,该美女真的很疯狂,希望和大家一起坚持51天做一件事情,我加入这个队伍,希望坚持51天每天写一篇技术文章.关注她的微信公众号: ...
- C/C++学习之基础-001
1.C++虚函数的工作原理 虚函数(virtual function)需要虚函数表(virtual table)才能实现.如果一个类有函数声明成虚拟的,就会生成一个虚函数表,存放这个类的虚函数地址.若 ...
- python 绘图工具 matplotlib 入门
转自: http://www.cnblogs.com/kaituorensheng/p/3440273.html matplotlib 是python最著名的绘图库,它提供了一整套和matlab相似的 ...
- (spring-第1回【IoC基础篇】)Spring容器中Bean的生命周期
日出日落,春去秋来,花随流水,北雁南飞,世间万物皆有生死轮回.从调用XML中的Bean配置信息,到应用到具体实例中,再到销毁,Bean也有属于它的生命周期. 人类大脑对图像的认知能力永远高于文字,因此 ...
- 一个经典例子让你彻彻底底理解java回调机制
转帖请注明本文出自xiaanming的博客(http://blog.csdn.net/xiaanming/article/details/17483273),请尊重他人的辛勤劳动成果,谢谢 所谓回调: ...
- Java_oop_继承
不用多久,就会升职加薪,当上总经理,出任CEO,迎娶白富美,走上人生巅峰.想想还有点小激动呢, OK,睡醒了,我们说到继承就先来学习一下Java中继承的语法. public SubClass exte ...
- 7、C#基础整理(类)
String类 概念:是一个class类型的类,里面包含许多处理字符串的方法和属性 1.length方法. 例: string s = "hello"; Console.Write ...