原题地址

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. freefilesync 7 使用

    官方下载地址:http://www.freefilesync.org/download.php 1.打开FreeFileSync 设置左右的文件夹,设置过滤规则,设置同步方式(双向.单向),执行同步 ...

  2. Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException:

    七月 17, 2014 4:56:01 下午 org.apache.catalina.core.StandardWrapperValve invoke SEVERE: Servlet.service( ...

  3. C++求等比数列之和

    题目内容:已知q与n,求等比数列之和:1+q+q2+q3+q4+……+qn. 输入描述:输入数据不多于50对,每对数据含有一个整数n(1<=n<=20).一个小数q(0<q<2 ...

  4. leetcode刷题笔记

    (1)Best Time to Buy and Sell Stock Total Accepted: 10430 Total Submissions: 33800My Submissions Say ...

  5. Sql Server数据库之通过SqlBulkCopy快速插入大量数据

    废话不多说,直接上代码 /// <summary> /// 海量数据插入方法 /// </summary> /// <param name="connectio ...

  6. C# 标准查询表达式

    一.标准查询运算符 1.C#提供了标准查询运算符,例如我想选择专利一系列(pantents)中以年份19开头的专利,可以用如下语句: IEnumerable<Patent> pantent ...

  7. AngularJs记录学习01

    <!doctype html> <html ng-app="myapp"> <head> <meta http-equiv="C ...

  8. C 关于二叉查找树的回顾,并简述结构接口设计

    前言  最经想改写C用的配置读取接口, 准备采用hash或二叉树提到原先用的链表,提高查找效率.就回顾一下二叉树,这里分享一下二叉查找树,代码很精简的, 适合学习运用二叉树查找. 需要基础 1.具备C ...

  9. 菜鸟学习Hibernate——简单的增、删、改、查操作

    上篇博客利用Hibernate搭建起一个简单的例子,把数据库的映射显示了出来在上一篇的博客基础上这篇博客讲述如何利用Hinbernate框架实现简单的数据库操作. 1.加入junit.jar 2.新建 ...

  10. Python数学运算

    python中的加减乘除比其他的语言简单,不需要对其赋值变量 (1)加减乘除 ) #加法 ) #减法 ) #乘法 ) #除法 5.0 ) #乘方 (2)判断 判断返回的是True或者False ) # ...