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.

C++

class Solution {
public:
int longestConsecutive(vector<int>& nums) {
int len = nums.size();
unordered_set<int> se;
for(int i=0;i<len;i++)
se.insert(nums[i]);
int maxLen = 0;
for(int i=0;i<len;i++){
int tmpLen = 1;
for(int tmp = nums[i] + 1;se.count(tmp);tmpLen++,tmp++)
se.erase(tmp);
for(int tmp = nums[i] - 1;se.count(tmp);tmpLen++,tmp--)
se.erase(tmp);
maxLen = max(tmpLen, maxLen);
}
return maxLen;
}
};

longest-consecutive-sequence leetcode C++的更多相关文章

  1. 128. Longest Consecutive Sequence(leetcode)

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  2. Binary Tree Longest Consecutive Sequence -- LeetCode

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

  3. Longest Consecutive Sequence [LeetCode]

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  4. Longest Consecutive Sequence——Leetcode

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  5. Longest Consecutive Sequence leetcode java

    题目: Given an unsorted array of integers, find the length of the longest consecutive elements sequenc ...

  6. [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

  7. [LeetCode] Longest Consecutive Sequence 求最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  8. LeetCode Binary Tree Longest Consecutive Sequence

    原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/ 题目: Given a binary t ...

  9. 【LeetCode OJ】Longest Consecutive Sequence

    Problem Link: http://oj.leetcode.com/problems/longest-consecutive-sequence/ This problem is a classi ...

  10. [LeetCode] 128. Longest Consecutive Sequence 解题思路

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

随机推荐

  1. leetcode 盛水最多的容器 解析

    采用双指针法: 主要思想:定义头尾两个指针,分别向中间遍历,当相遇时结束循环.存储每一次遍历容器盛水的最大容量,并不断更新. 盛水的最大容量为 左右指针高度的最小值 乘以 左右指针的距离即宽度. 则可 ...

  2. PHP的switch和ifelse谁更快?

    对于多个if条件判断的情况下,我们使用switch来代替ifelse对于代码来说会更加的清晰明了,那么他们的效率对比呢?从PHP手册中发现有人已经对比过了,自己也用他的代码进行了实验: $s = ti ...

  3. npm WARN ajv-keywords@2.1.1 requires a peer of ajv@^5.0.0 but none is installed. You must install peer dependencies yourself.

    解决: npm install -g npm-install-peers npm install -g npm npm i ajv 但是好像没啥用

  4. java中避免集合死链调用

    目录 1. 前言 2. 场景 3. 环境 3.1 开发环境准备 3.2 数据准备 3.2.1 Mysql数据库表及数据 3.2.2 redis库数据 4. 解决方式 5.完整代码 5.1Model 5 ...

  5. greedy algorithm, insertion sort, quick sort

    always makes the choice that seems to be the best at that moment. Example #1: @function:  scheduling ...

  6. python的列表和java的数组有何异同

    今天面试被问到,自己学习一下. python的列表是可变长的,定义时不需要指定长度:pyhton是弱对象类型,python的列表存储的数据类型可以不相同:python的列表更加灵活,如可以通过''命令 ...

  7. PHP统计当前网站的访问人数,访问信息,被多少次访问。

    <?php  header('Content-type:text/html;charset=utf-8'); //统计流量(人数,访问次数,用户IP) //假设用户访问,得到IP地址 $remo ...

  8. 鸿蒙内核源码分析(用栈方式篇) | 程序运行场地谁提供的 | 百篇博客分析OpenHarmony源码 | v20.04

    百篇博客系列篇.本篇为: v20.xx 鸿蒙内核源码分析(用栈方式篇) | 程序运行场地谁提供的 | 51.c.h .o 精读内核源码就绕不过汇编语言,鸿蒙内核有6个汇编文件,读不懂它们就真的很难理解 ...

  9. AT2161-[ARC065D]シャッフル/Shuffling【dp】

    正题 题目链接:https://www.luogu.com.cn/problem/AT2161 题目大意 长度为\(n\)的\(0/1\)串,\(m\)个区间,你可以按照顺序任意排列区间中的数字,求最 ...

  10. RuntimeError: DataLoader worker (pid 18255) is killed by signal: Killed.

    RuntimeError: DataLoader worker (pid 18255) is killed by signal: Killed. 通过观察内存发现,数据加载过程中内存会被耗尽.