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最长连续序列的更多相关文章

  1. 128. Longest Consecutive Sequence最长连续序列

    [抄题]: Given an unsorted array of integers, find the length of the longest consecutive elements seque ...

  2. 298. Binary Tree Longest Consecutive Sequence最长连续序列

    [抄题]: Given a binary tree, find the length of the longest consecutive sequence path. The path refers ...

  3. [Leetcode] Longest consecutive sequence 最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  4. [LeetCode] 128. Longest Consecutive Sequence 求最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  5. LeetCode 128 Longest Consecutive Sequence 一个无序整数数组中找到最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence.Fo ...

  6. [LeetCode] 128. Longest Consecutive Sequence 解题思路

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  7. leetcode 128. Longest Consecutive Sequence ----- java

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  8. Java for LeetCode 128 Longest Consecutive Sequence

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  9. Leetcode 128. Longest Consecutive Sequence (union find)

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Y ...

随机推荐

  1. 我要重新开始学习C++了!

    C++实在是博大精深!之前总不想读厚厚的C++ Primer. 然而,现在的水平真的只是初学者!只是因为写的代码太简单,所以没有用到一些特性.可以说还是门外汉! 写笔记!

  2. 关于maven中的快照版本(snapshot)与正式版本(release)解析。

    Maven中建立的依赖管理方式基本已成为Java语言依赖管理的事实标准,Maven的替代者Gradle也基本沿用了Maven的依赖管理机制.在Maven依赖管理中,唯一标识一个依赖项是由该依赖项的三个 ...

  3. 1.ECS(CentOS7)主机名修改命令

    命令:hostnamectl 使用--help参数查看hostnamectl里面每个参数对应的含义: [root@localhost ~]# hostnamectl --help Query or c ...

  4. 跨域(四)——document.domain

    浏览器有一个合法的性质:一个页面可以设置document.domain为当前子域或比当前子域更高级的域.一般顶级就到了根域,如果设置为其他域,浏览器就会报权限错误. 利用这个性质,我们可以通过设置do ...

  5. 卸载数据盘、更改Inodes

    更改inodes  会格式化数据库,记得先备份 1.fuser -m -v /dev/vdb查看哪些进程正在占用数据库 2.然后kill 掉进程 3.umount /data1/ 4.mkfs.ext ...

  6. 【转】WinDbg调试器:启动程序时自动连接调试器方法

    当我们调试服务进程或子进程时,无法直接用调试加载进程的方式启动,此时需要在启动程序时自动连接调试器方法: 第一步:注册表展开到HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft ...

  7. 微信小程序---模版

    微信小程序用的是否娴熟,会灵活使用模版很重要. 新建一个template文件,做一个step模版. <template name="top"> <view cla ...

  8. Rx操作符

    [Rx操作符] 1.Observable.from()方法,它接收一个集合作为输入,然后每次输出一个元素给subscriber: Observable.from("url1", & ...

  9. yum被锁定:Another app is currently holding the yum lock; waiting for it to exit…

    yum被锁定无法使用,错误信息截图如下: 解决方法:rm -rf /var/run/yum.pid 来强行解除锁定,然后你的yum就可以运行了

  10. SQLMAP自动注入(三):参数介绍

    --delay延时扫描 --scope 从burpsuit日志中过滤日志内容,通过正则表达式筛选扫描目标,19开头,尾数为1.11.121.221的目标 --level=3 会检查user-agent ...