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. [WPF] 将普通的Library工程,改造成WPF Custom Control 的Library

    1. 添加References PresentationCore PresentationFramework System.Xaml WindowsBase2. 修改AssemblyInfo.xsus ...

  2. js如何获取object类型里的键值

    最近遇到一个问题: var obj = {"name1":"张三","name2":"李四"}; var key = & ...

  3. javascript 如何继承父类

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  4. [原创作品]手把手教你怎么写jQuery插件

    这次随笔,向大家介绍如何编写jQuery插件.啰嗦一下,很希望各位IT界的‘攻城狮’们能和大家一起分享,一起成长.点击左边我头像下边的“加入qq群”,一起分享,一起交流,当然,可以一起吹水.哈,不废话 ...

  5. Leetcode_num3_Same Tree

    题目: Given two binary trees, write a function to check if they are equal or not. Two binary trees are ...

  6. Eclipse导出jar包Unity打包错误

    前几天接SDK使用的是Android Studio昨天打开AndroidStudio后自动更新了gradler然后失败了然后AndroidStudio就挂了.就是用之前的方法Eclipse到处jar包 ...

  7. 多路复用I/O select()

    select(),poll(),epoll()的总结:http://www.cnblogs.com/Anker/p/3265058.html 在socket编程中,仅仅使用connect,accept ...

  8. NVL函数(NVL,NVL2,NULLIF,COALESCE)

    NVL 语法:NVL( expr1, expr2) 功能:如果expr1为NULL,则NVL函数返回expr2的值,否则返回expr1的值,如果两个参数的都为NULL ,则返回NULL. 注意事项:e ...

  9. tomcat配置数据源

    1.修改conf下的context.xml,在<context>标签中添加: <Resource name="jdbc/soa" auth="Conta ...

  10. CSS 实现三角形、梯形、等腰梯形

    三角形 ; width: 0px; border-width: 0px 30px 45px 145px; border-style: none solid solid; border-color: t ...