原题地址

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. Java TCP异步数据接收

    之前一直采用.Net编写服务端程序,最近需要切换到Linux平台下,于是尝试采用Java编写数据服务器.TCP异步连接在C#中很容易实现,网上也有很多可供参考的代码.但Java异步TCP的参考资料较少 ...

  2. centos6.7下编译安装lamp环境

    编译C源代码: 前提:提供开发工具及开发环境 通过“包组”提供开发组件,CentOS 6: "Development Tools", "Server Platform D ...

  3. Jquery数组操作技巧

    Jquery对数组的操作技巧. 1. $.each(array, [callback]) 遍历[常用]  解释: 不同于例遍 jQuery 对象的 $.each() 方法,此方法可用于例遍任何对象(不 ...

  4. thinkphp 字段静态验证$_validate中错误提醒多语言化写成{%LANGUATE}的原因

    class UserModel extends Model{ protected $_validate =  array( array('account', 'require', '{%LANGUAG ...

  5. pure的bug记录2

    <select id="stacked-state" style=" font-family: "Microsoft YaHei"; " ...

  6. Linq的一些记录

    1. IQueryable接口与IEnumberable接口的区别:  IEnumerable<T> 泛型类在调用自己的SKip 和 Take 等扩展方法之前数据就已经加载在本地内存里了, ...

  7. java 通过zxing生成二维码

    1.基本类提供二维码生成工具类 package com.green.util; import java.awt.image.BufferedImage; import java.io.ByteArra ...

  8. .net 的生成操作

    生成操作(BuildAction) 属性:BuildAction 属性指示 Visual Studio .NET 在执行生成时对文件执行的操作. BuildAction 可以具有以下几个值之一: 无( ...

  9. netlink

    http://blog.csdn.net/zirconsdu/article/details/8569193 http://www.xuebuyuan.com/1725837.html netlink ...

  10. 数据持久化-Plist文件写入

    数据持久化,常见4种:归档,plist文件,sqlite,coreData.今天复习的是plist文件读写. // // ViewController.m // Test_Plist // // Cr ...