[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 ...
随机推荐
- 初识web.xml文件
做了那么久的web项目都没有花心思了充分解下这个文件有什么用,看项目配制是否都差不多呢 ======================================================== ...
- 2319__1.5.3 Superprime Rib 特殊的质数肋骨
[Submit][Status][Forum] Description 农民约翰母牛总是产生最好的肋骨. 你能通过农民约翰和美国农业部标记在每根肋骨上的数字认出它们. 农民约翰确定他卖给买方的是真正的 ...
- javascript中函数作用域和声明提前
javascript不像java等其他强类型语句,没有块级作用域(括号内的代码都有自己的作用域,变量在声明它们的代码段之外不可见)一说,但有自己的独特地方,即函数作用域. 函数作用域:变量在声明它们的 ...
- Idiom: a Lot on my Plate
Idiom: a Lot on my Plate Share Tweet Share Tagged With: Idioms I’ve got a lot on my plate. American ...
- PHP脚本不报错的两点原因
-------------------------------------------------------------------------------------------------- P ...
- 使用jQuery+huandlebars遍历展示对象中的数组
兼容ie8(很实用,复制过来,仅供技术参考,更详细内容请看源地址:http://www.cnblogs.com/iyangyuan/archive/2013/12/12/3471227.html) & ...
- app与jvm 反向代理时config的设置(用于在web页面显示npm(就如tomcat)产生的页面)
dev: { // Various Dev Server settings contentBase: ROOT, host: ip, port: 8084, //此端口为任意设置,不重复即可,为 ...
- Docker 指定容量
vim /etc/sysconfig/docker-storage加入以下命令 DOCKER_STORAGE_OPTIONS="--storage-driver devicemapper - ...
- centos 安装python3.6
环境准备 yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel 首先去官网下 ...
- String intern 方法 jdk中的描述
一个初始为空的字符串池,它由类 String 私有地维护. 当调用 intern 方法时,如果池已经包含一个等于此 String 对象的字符串(用 equals(Object) 方法确定),则返回池中 ...