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 ...
随机推荐
- puts()和gets()函数
puts()和gets()函数 1. puts()函数 puts()函数用来向标准输出设备(屏幕)写字符串并换行, 其调用格式为: puts(s); 其中s为字符串变量(字符串数组名或字符串指针). ...
- P6268 [SHOI2002]舞会
题目描述 Link 某学校要召开一个舞会.已知学校所有 \(n\) 名学生中,有些学生曾经互相跳过舞.当然跳过舞的学生一定是一个男生和一个女生.在这个舞会上,要求被邀请的学生中的任何一对男生和女生互相 ...
- 【题解】Ehab the Xorcist
\(\color{red}{Link}\) \(\color{blue}{\text{Solution:}}\) 题目要求构造一个最短的序列,使得异或和为\(u\),数列和为\(v\). 那么,因为是 ...
- 【从零开始撸一个App】Kotlin
工欲善其事必先利其器.像我们从零开始撸一个App的话,选择最合适的语言是首要任务.如果你跟我一样对Java蹒跚的步态和僵硬的语法颇感无奈,那么Kotlin在很大程度上不会令你失望.虽然为了符合JVM规 ...
- 洛谷 P3413 【萌数】
敲完这篇题解,我就,我就,我就,嗯,好,就这样吧... 思路分析: 首先我们要知道一个回文串的性质--假如说一个[l-1,r+1]的串是回文的,那么[l,r]一定也是回文的. 所以我们只要记录前一个数 ...
- navicat 生成注册码( 仅供学习使用 )
前言,由于navicat使用比较顺手,刚好前段时间试用期到,又看看了怎么生成注册码,特地记录下使用 . 1.运行 找到 navicat 文件(exe) 2.生成注册文件(报错好,后续会用到) 3.断网 ...
- 多测师讲解jmeter _基本介绍_(001)高级讲师肖sir
jmeter讲课课程 一.Jmeter简介 Jmeter是由Apache公司开发的一个纯Java的开源项目,即可以用于做接口测试也可以用于做性能测试. Jmeter具备高移植性,可以实现跨平台运行. ...
- 多测师讲解自动化测试 _RF分配id_高级讲师肖sir
1.Assign Id To Element.
- 阿里百秀后台管理项目笔记 ---- Day01
摘要 在此记录一下阿里百秀项目的教学视频的学习笔记,部分页面被我修改了,某些页面效果会不一样,基本操作是一致的,好记性不如烂笔头,加油叭!!! step 1 : 整合全部静态页面 将静态页面全部拷贝到 ...
- JDBC的学习(一)
JDBC的学习(一) 概念 所谓英文简写的意思是:Java DataBase Connectivity ,即 Java数据库的连接,用Java语言来操作数据库 本质 简单的来说,就是写这个JDBC的公 ...