23longest-consecutive-sequence
题目描述
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.
For example,
Given[100, 4, 200, 1, 3, 2],
The longest consecutive elements sequence is[1, 2, 3, 4]. Return its length:4.
Your algorithm should run in O(n) complexity.
输出
4
class Solution {
public:
/**
*
* @param num int整型vector
* @return int整型
*/
int longestConsecutive(vector<int>& num) {
// write code here
set<int> Set(num.begin(),num.end());
int result=1;
for (int x:num)
{
if (Set.find(x)==Set.end())
continue;
Set.erase(x);
int l=x-1,r=x+1;
while (Set.find(l)!=Set.end())
Set.erase(l--);
while (Set.find(r)!=Set.end())
Set.erase(r++);
result=max(result,r-l-1);
}
return result;
}
};
23longest-consecutive-sequence的更多相关文章
- [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- [LeetCode] Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- Binary Tree Longest Consecutive Sequence
Given a binary tree, find the length of the longest consecutive sequence path (连续的路径,不是从小到大). The pa ...
- 【leetcode】Longest Consecutive Sequence(hard)☆
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- 128. Longest Consecutive Sequence(leetcode)
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- [LintCode] Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. H ...
- LeetCode Binary Tree Longest Consecutive Sequence
原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/ 题目: Given a binary t ...
- 24. Longest Consecutive Sequence
Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...
- 【leetcode】Longest Consecutive Sequence
Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...
- 【LeetCode OJ】Longest Consecutive Sequence
Problem Link: http://oj.leetcode.com/problems/longest-consecutive-sequence/ This problem is a classi ...
随机推荐
- RHSA-2018:3665-重要: NetworkManager 安全更新
[root@localhost ~]# cat /etc/redhat-release CentOS Linux release 7.2.1511 (Core) 修复命令: 使用root账号登陆She ...
- ansible-playbook文件复用
1. ansible-playbook文件复用 1.1) include 和 import区别 include(动态):在运行时导入 --list-tags,--list-tasks不会显示到输出 ...
- pytest文档40-pytest.ini配置用例查找规则(面试题)
前言 面试题:pytest如何执行不是test开头的用例?如执行 xxx_*.py这种文件的用例. pytest.ini 配置文件可以修改用例的匹配规则. pytest命令行参数 cmd打开输入pyt ...
- Anno 框架 增加缓存、限流策略、事件总线、支持 thrift grpc 作为底层传输
github 地址:https://github.com/duyanming/dymDemo dym 分布式开发框架 Demo 熔断 限流 事件总线(包括基于内存的.rabbitmq的) CQRS D ...
- CTSC2010
星际旅行 https://www.luogu.com.cn/problem/P4189 题目:且每个星球的\(H_i\)大于等于与该星球直接相连的星球数(即度数). 想到先从根到所有点都走一遍,然后贪 ...
- linux(centos8):使用tree命令查看目录结构
一,tree命令的用途 tree命令以树状图列出文件目录结构 说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest 对应的源 ...
- Java9系列第6篇-Stream流API的增强
我计划在后续的一段时间内,写一系列关于java 9的文章,虽然java 9 不像Java 8或者Java 11那样的核心java版本,但是还是有很多的特性值得关注.期待您能关注我,我将把java 9 ...
- Drone 安装教程
Drone 安装教程 一. CentOS设置 1. 更换阿里源 curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/ ...
- C++switch结构
- docker 启动容器restart 策略
docker 运行容器时使用--restart 参数可以指定一个restart策略,来指定容器应该如何重启,或不应该重启,当容器启用restart策略时,将会载docker ps 显示up 或者res ...