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 ...
随机推荐
- 【线段树 扫描线 二维数点】loj#6276. 果树
路径计数转成二维数点很妙啊 题目描述 NiroBC 姐姐是个活泼的少女,她十分喜欢爬树,而她家门口正好有一棵果树,正好满足了她爬树的需求. 这颗果树有 $N$ 个节点,标号 $1 \ldots N$ ...
- pandas交叉表和透视表及案例分析
一.交叉表: 作用: 交叉表是一种用于计算分组频率的特殊透视图,对数据进行汇总 考察预测数据和正式数据的对比情况,一个作为行,一个作为列 案例: 医院预测病人病情: 真实病情如下数组(B:有病,M:没 ...
- IntelliJ IDEA 配置 Tomcat 运行web项目
运行前提: 配置好 Java 的运行环境 配置好 Tomcat 安装 IntelliJ IDEA 开始 1.创建项目并配置 关于配置SDK,等建完项目再说 没有配置SDK的话 会出现下面的弹框,点击 ...
- MYSQL存储过程,函数,光标
存储过程 MySQL中,创建存储过程的基本形式如下: CREATE PROCEDURE sp_name ([proc_parameter[,...]]) [characteristic ...] ro ...
- 笔记--Day2--python基础2
一.鸡汤 1.提高自我修养 2.人丑就要多读书 3.多走走,开拓眼界 二.目录: 1.列表.元组操作 2.字符串操作 3.字典操作 dict是无序的 key必须是唯一的 4.集合操作 集合是一个无序的 ...
- python处理excel总结
工作中,大家经常会使用excel去处理数据以及展示,但是对于部分工作我们可以借助程序帮忙实现,达到高效解决问题的效果,比如将接口返回的json解析并保存结果到excel中,按一定规律处理excel中的 ...
- Linux下配置Django_Apache_Mysql环境(CentOS 7.5)
本文将介绍如何在Linux上部署Django + Mysql + Apache环境.我们知道,Django内置的http服务器只能工作在单线程下,做开发和调试时候是可以的,但是生产环境通常都会有多用户 ...
- paper:synthesizable finit state machine design techniques using the new systemverilog 3.0 enhancements之onehot coding styles(encoded-parameter style with registered outputs不推荐但是经常有人写这样的代码)
这样写法,不利与综合,case语句中比较也是full-vector比较.
- python 类的使用
目录 类的继承 类的派生 类的组合 菱形继承问题 多态与多态性 dataclass的使用 类的继承 什么是继承,在生活中,子承父业,父亲和儿子就是继承的关系 在python中,父类和子类(派生类),父 ...
- AD采样求平均STM32实现
iADC_read(, &u16NTC_1_Sample_Val_ARR[]); == ui8FirstSampleFlag) { ; i<; i++) { u16NTC_1_Sampl ...