http://oj.leetcode.com/problems/longest-consecutive-sequence/

起初想的是排序,查了下O(n)的排序算法有计数排序、基数排序、桶排序。后来考虑到数据的范围不知道,并且还有可能是负数,这几种方法都不太适用。

之后想到了容器,Map、Set的查找是哈希查找,复杂度为O(1).

#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std; class Solution {
public:
unordered_set<int> dict; int findLongestConsective(int num)
{
int ans = ;
int temp = num;
//往上找
while(dict.find(temp)!=dict.end())
{
ans++;
dict.erase(temp);
temp++;
}
//往下找
while(dict.find(num-)!=dict.end())
{
ans++;
dict.erase(num-);
num--;
}
return ans;
}
int longestConsecutive(vector<int> &num) { for(int i = ;i<num.size();i++)
dict.insert(num[i]); int maxLen = ;
for(int i = ;i<num.size();i++)
{
int t = findLongestConsective(num[i]);
if(t>maxLen)
maxLen = t;
}
return maxLen;
}
}; int main()
{
vector<int> num;
num.push_back();
num.push_back();
num.push_back();
num.push_back();
num.push_back();
num.push_back();
Solution myS;
int ans = myS.longestConsecutive(num);
cout<<ans<<endl;
}

重点是思路的转换,以及 set 的应用。

LeetCode OJ--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. 【leetcode】Longest Consecutive Sequence

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

  4. Java for LeetCode 128 Longest Consecutive Sequence

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

  5. 【leetcode】Longest Consecutive Sequence(hard)☆

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

  6. [Leetcode][JAVA] Longest Consecutive Sequence

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

  7. leetcode 128. Longest Consecutive Sequence ----- java

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

  8. [leetcode]128. Longest Consecutive Sequence最长连续序列

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

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

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

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

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

随机推荐

  1. 【动态规划】bzoj1705: [Usaco2007 Nov]Telephone Wire 架设电话线

    可能是一类dp的通用优化 Description 最近,Farmer John的奶牛们越来越不满于牛棚里一塌糊涂的电话服务 于是,她们要求FJ把那些老旧的电话线换成性能更好的新电话线. 新的电话线架设 ...

  2. 洛谷 4219/BZOJ 4530 大融合

    4530: [Bjoi2014]大融合 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 990  Solved: 604[Submit][Status] ...

  3. Voyager的路由

    修改默认的后台登录路由 打开web.php,把prefix值改为你想设置的值,如back: Route::group(['prefix' => 'back'], function () { Vo ...

  4. 【Linux】开放指定端口设置

    这里以开放tomcat的8080端口为例 1.开放Linux的8080端口 vi /etc/sysconfig/iptables 进入编辑页面,在指定位置新增以下配置 -A INPUT -m stat ...

  5. 【windows】win7 sp1 系统语言中英文切换

    注:Windows 7 Ultimate and Windows 7 Enterprise (旗舰版和企业版) 可以直接在控制面板/地区和语言中修改显示语言,其他系统不行 进入网站下载相关的MUI包安 ...

  6. matplotlib学习记录 三

    # 绘制自己和朋友在各个年龄的女友数量的折线图 from matplotlib import pyplot as plt # 让matplotlib能够显示中文 plt.rcParams['font. ...

  7. Django之URL

    URL是用户请求路径与views视图处理函数的一个映射 简单的路由配置及实现 这里是pycharm编辑开发为例,新建的django项目,会在url.py下自动生成这样一段代码: from django ...

  8. HDU 3727 Jewel 主席树

    题意: 一开始有一个空序列,然后有下面四种操作: Insert x在序列尾部加入一个值为\(x\)的元素,而且保证序列中每个元素都互不相同. Query_1 s t k查询区间\([s,t]\)中第\ ...

  9. 【原创】Mysql中事务ACID实现原理

    引言 照例,我们先来一个场景~ 面试官:"知道事务的四大特性么?" 你:"懂,ACID嘛,原子性(Atomicity).一致性(Consistency).隔离性(Isol ...

  10. Jquery+Ajax+asp.net+sqlserver-编写的通用邮件管理(有源码)

    开始 邮件管理通常用在各个内部系统中,为了方便快捷的使用现有的代码开发一个邮件管理系统而诞生的. 准备条件 这是我的设计表结构,大家一看就懂了 --邮件接收表CREATE TABLE [dbo].[T ...