Question

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.

Solution

一开始尝试用segment tree, heap等,发现时间复杂度不能达到线性。

后来参考别人答案,发现用HashSet计数的方法。

程序比较简单。

1. 遍历一遍数组,将所有元素存入set

2. 遍历数组。对于当前元素x,看x+1和x-1是否在set中。如果在,则remove,并且一直找到这个区间的边界。

 public class Solution {
public int longestConsecutive(int[] nums) {
Set<Integer> set = new HashSet<Integer>();
for (int i : nums) {
set.add(i);
}
int max = 0;
int count;
for (int i : nums) {
if (set.isEmpty()) {
break;
}
count = 1;
int right = i + 1;
int left = i - 1;
set.remove(i);
// find right
while (set.contains(right)) {
set.remove(right);
count++;
right++;
}
// find left
while (set.contains(left)) {
set.remove(left);
count++;
left--;
}
max = Math.max(max, count);
}
return max;
}
}

Longest Consecutive Sequence 解答的更多相关文章

  1. [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

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

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

  3. Binary Tree Longest Consecutive Sequence

    Given a binary tree, find the length of the longest consecutive sequence path (连续的路径,不是从小到大). The pa ...

  4. 128. Longest Consecutive Sequence(leetcode)

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

  5. [LintCode] Longest Consecutive Sequence 求最长连续序列

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

  6. LeetCode Binary Tree Longest Consecutive Sequence

    原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/ 题目: Given a binary t ...

  7. 24. Longest Consecutive Sequence

    Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...

  8. 【leetcode】Longest Consecutive Sequence

    Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...

  9. 【LeetCode OJ】Longest Consecutive Sequence

    Problem Link: http://oj.leetcode.com/problems/longest-consecutive-sequence/ This problem is a classi ...

随机推荐

  1. HDU4821---字符串hash,map判重

    这是2013年长春区域赛的铜牌题...然而第一次做的时候一直觉得会超时的..最后才知道并没有想象中的那么恐怖: 这题有两个注意的地方: (1)h[i] = h[i-1] * seed + s[i] - ...

  2. WebService-相关概念介绍

    WebService学习总结(二)——WebService相关概念介绍 一.WebService是什么? 1. 基于Web的服务:服务器端整出一些资源让客户端应用访问(获取数据) 2. 一个跨语言.跨 ...

  3. CSS介绍

    从HTML被发明开始,样式就以各种形式存在.不同的浏览器结合它们各自的样式语言为用户提供页面效果的控制.最初的HTML只包含很少的显示属性. 随着HTML的成长,为了满足页面设计者的要求,HTML添加 ...

  4. 每天一点Swift(五)控制器的生命周期和SizeClass

    字数358 阅读19 评论0 喜欢0 初始化init-->awakeFromNib--> prepare a segue --> SB去设置outlets --> viewDi ...

  5. HDU 5274(树链剖分)

    树链剖分第一题QAQ,纪念下 #pragma comment(linker, "/STACK:102400000,102400000") #include <iostream ...

  6. Hadoop集群运行JNI程序

    要在Hadoop集群运行上运行JNI程序,首先要在单机上调试程序直到可以正确运行JNI程序,之后移植到Hadoop集群就是水到渠成的事情. Hadoop运行程序的方式是通过jar包,所以我们需要将所有 ...

  7. C++设计模式---Strategy模式

    一.前言 学习的第一个设计模式!不知道理解的对不对,期望大家一起多交流~ Strategy模式:策略模式,定义了算法族,分别封装起来,此模式可以让算法的变化独立于使用算法的客户.Strategy模式将 ...

  8. ORA-24324、ORA-12560、ORA-12514

    SQL> startup ERROR: ORA-24324: 未初始化服务句柄 ORA-01041: 内部错误, hostdef 扩展名不存在. SQL> conn sys /nolog; ...

  9. .NET基础拾遗(6)特性

    1 神马是特性?如何自定义一个特性? (1)特性是什么     特性是一个对象,可以加载到程序集及程序集的对象中,这些对象包括 程序集本身.模块.类.接口.结构.构造函数.方法.方法参数等,加载了特性 ...

  10. (转)sql server 2008 不允许保存更改,您所做的更改要求删除并重新创建以下表 的解决办法

    启动SQL Server 2008 Management Studio 工具菜单----选项----Designers(设计器)----阻止保存要求重新创建表的更改  取消勾选即可.