LeetCode OJ--Longest Consecutive Sequence ***
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 ***的更多相关文章
- [LeetCode] 128. Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- [LeetCode] 128. Longest Consecutive Sequence 解题思路
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- 【leetcode】Longest Consecutive Sequence
Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...
- Java for LeetCode 128 Longest Consecutive Sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- 【leetcode】Longest Consecutive Sequence(hard)☆
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- [Leetcode][JAVA] Longest Consecutive Sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- leetcode 128. Longest Consecutive Sequence ----- java
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- [leetcode]128. Longest Consecutive Sequence最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Y ...
- Leetcode 128. Longest Consecutive Sequence (union find)
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Y ...
- LeetCode 128 Longest Consecutive Sequence 一个无序整数数组中找到最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.Fo ...
随机推荐
- 【费用流】bzoj1834: [ZJOI2010]network 网络扩容
还是稍微记一下这个拆点模型吧 Description 给定一张有向图,每条边都有一个容量C和一个扩容费用W.这里扩容费用是指将容量扩大1所需的费用. 求: 1.在不扩容的情况下,1到N的最大流: ...
- 无法重启ssh
rm /dev/null mknod /dev/null c 1 3 chmod 666 /dev/null
- java上传附件,批量下载附件(一)
上传附件代码:借助commons-fileupload-1.2.jar package com.str; import java.io.BufferedInputStream;import java. ...
- python3中urllib库的request模块详解
刚刚接触爬虫,基础的东西得时时回顾才行,这么全面的帖子无论如何也得厚着脸皮转过来啊! 原帖地址:https://www.2cto.com/kf/201801/714859.html 什么是 Urlli ...
- 笔记-python-centos环境下安装配置
笔记-python-centos环境下安装配置 1. 准备工作 环境准备 centos6.5 mini,已有python 2.6.6 下载源码包 Python官网下载Gzipped sour ...
- P3391 【模板】文艺平衡树FHQ treap
P3391 [模板]文艺平衡树(Splay) 题目背景 这是一道经典的Splay模板题——文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转 ...
- JS实现——贪吃蛇
把以下代码保存成Snake.html文件,使用Google或360浏览器打开 <!DOCTYPE HTML> <html> <head> <meta char ...
- TPS限流
限流是高可用服务需要具备的能力之一 ,粗暴简单的就像我们之前做的并发数控制.好一点的有tps限流,可用令牌桶等算法实现.<亿级流量网站架构核心技术>一书P67限流详解也有讲.dubbo提供 ...
- Jmeter生成8位不重复的随机数
jmeter的time函数${__time(,)} : 默认该公式精确到毫秒级别, 13位数 ${__time(/1000,)} : 该公式精确到秒级别, 10位数 ${__time(yyyy- ...
- PHP 函数 ignore_user_abort()
ignore_user_abort 设置与客户机断开是否会终止脚本的执行. 本函数返回 user-abort 设置的之前的值(一个布尔值). int ignore_user_abort ([ st ...