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 ...
随机推荐
- 【题解】[USACO09NOV]A Coin Game S
Link \(\text{Solution:}\) 菜鸡自己想出来了状态设计,但是没有实现出来--菜死了 设\(dp[i][j]\)表示该选第\(i\)个,最多选\(j\)个的最优解.注意这里的定义仅 ...
- ubuntu19.10如何添加开机启动项
$sudo vi /lib/systemd/system/rc-local.service内容如下[Unit]Description=/etc/rc.local CompatibilityDocume ...
- golang API 请求队列
概要 实现思路 使用方法 启动队列服务 使用队列服务 概要 在调用第三方 API 的时候, 基本都有访问限速的限制条件. 第三方的 API 有多个的时候, 就不太好控制访问速度, 常常会导致 HTTP ...
- 【博弈论】51Nod 1534 棋子游戏
题目内容 波雷卡普和瓦西里喜欢简单的逻辑游戏.今天他们玩了一个游戏,这个游戏在一个很大的棋盘上进行,他们每个人有一个棋子.他们轮流移动自己的棋子,波雷卡普先开始.每一步移动中,波雷卡普可以将他的棋子从 ...
- swoole协程通道channel
swoole 协程通道 为了协程直接互相通讯传递数据 和go的通道很相似 Co\run(function(){ $chan = new Swoole\Coroutine\Channel(1); Swo ...
- centos7 shell 安装docker redis mongodb 等
脚本下载: 百度网盘 提取码: wc4r 下载后上传到 服务器 chmod +x docker.sh sh docker.sh
- es7.8启动报错 说是主节点没找到
p.p1 { margin: 0; font: 11px Menlo; color: rgba(0, 0, 0, 1); background-color: rgba(255, 255, 255, 1 ...
- zookeeper在生产环境中的配置(zookeeper3.6)
一,zookeeper中日志的配置 1,快照文件snapshot的目录: dataDir=/data/zookeeper/data 存储快照文件snapshot的目录.默认情况下,事务日志也会存储在这 ...
- laravel或者lumen门面和服务提供者使用
关于laravel门面和服务提供者使用的一点见解,门面之词,不足之处,还请多多指教. 在laravel中,我们可能需要用到自己添加的类时,可以建立一个文件夹专门存放类文件,也可以使用lara ...
- python抓取动态验证码,具体第几帧数的位置静态图片
一.代码+注解 import os from PIL import Image import requests import io def save_img(): headers = { 'User- ...