原题地址

1. 把所有元素都塞到集合里
2. 遍历所有元素,对于每个元素,如果集合里没有,就算了,如果有的话,就向左向右拓展,找到最长的连续范围,同时在每次找的时候都把找到的删掉。这样做保证了同样的连续序列只会被遍历一次,从而保证时间复杂度。

时间复杂度O(n)

代码:

 int longestConsecutive(vector<int> &num) {
set<int> record;
int maxLength = ; for (auto n : num)
record.insert(n); while (!record.empty()) {
int len = ;
int n = *(record.begin());
record.erase(n);
for (int i = n - ; record.find(i) != record.end(); i--) {
len++;
record.erase(i);
}
for (int i = n + ; record.find(i) != record.end(); i++) {
len++;
record.erase(i);
}
maxLength = max(maxLength, len);
} return maxLength;

Leetcode#128 Longest Consecutive Sequence的更多相关文章

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

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

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

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

  3. Java for LeetCode 128 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 ----- java

    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. Y ...

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

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

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

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

  8. 128. Longest Consecutive Sequence(leetcode)

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

  9. 【LeetCode】128. Longest Consecutive Sequence

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

随机推荐

  1. sublime简单配置

    Preferences------->settings user { "font_face": "Courier New", "font_siz ...

  2. 重拾C,一天一点点_11

    命令行参数 在支持C语言的环境中,可以在程序开始执行时将命令行参数传递给程序. 调用主函数main时,它带有两个参数,第一个参数(argc,用于参数计数)的值表示运行程序时命令行参数的数目:第二个参数 ...

  3. 获取android SDCard存储大小

    //File path = Environment.getDataDirectory();//手机内置空间 1.获取SD卡的路径 File path = Environment.getExternal ...

  4. 13)Java static

    1.static变量      按照是否静态的对类成员变量进行分类可分两种:一种是被static修饰的变量,叫静态变量或类变量:另一种是没有被static修饰的变量,叫实例变量.两者的区别是:    ...

  5. 【转载】input 中 type='text' 的提交问题

    原文链接:http://www.nowamagic.net/html/html_AboutInputSummit.php 有时候我们希望回车键敲在文本框(input element)里来提交表单(fo ...

  6. Python学习教程(learning Python)--1.2.3 Python格式化输出百分比

    在有些情况下,需要百分比输出数据,我们可以继续使用Python内建函数format来实现百分比的数据输出. >>> print(format(0.5236, '.2%')) 其结果如 ...

  7. Linux内核学习笔记——VFS

    概念: ①硬链接:若一个 inode 号对应多个文件名,则称这些文件为硬链接.即硬链接就是同一个文件使用了多个别名.硬链接可由命令 link 或 ln 创建. 其特性: 文件有相同的 inode 及 ...

  8. Java并发编程实战---第六章:任务执行

    废话开篇 今天开始学习Java并发编程实战,很多大牛都推荐,所以为了能在并发编程的道路上留下点书本上的知识,所以也就有了这篇博文.今天主要学习的是任务执行章节,主要讲了任务执行定义.Executor. ...

  9. c++ 性能

    http://blog.sina.com.cn/s/blog_4a471ff601013vud.html http://www.linuxidc.com/Linux/2015-06/118874.ht ...

  10. Go append方法

    append用来将元素添加到切片末尾并返回结果.看代码: package main import "fmt" func main() { x := [],,} y := [],,} ...