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. 批大小、mini-batch、epoch的含义

    每次只选取1个样本,然后根据运行结果调整参数,这就是著名的随机梯度下降(SGD),而且可称为批大小(batch size)为 1 的 SGD. 批大小,就是每次调整参数前所选取的样本(称为mini-b ...

  2. C# Redis学习系列二:Redis基本设置

    上一篇:C# Redis学习系列一:Redis的认识.下载.安装.使用 一.redis 设置密码 使用下载好的 redis-cli.exe 指令: 1.设置密码: config set require ...

  3. PHP 合并2个链表

    输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则. <?php class ListNode{ var $val; var $next = NULL; ...

  4. CF5E-Bindian Signalizing【单调栈】

    正题 题目链接:https://www.luogu.com.cn/problem/CF5E 题目大意 圆上有\(n\)个山,两个山之间可以看到当且仅当它们之间的两条弧中有一条满足所有山都不高于它们两个 ...

  5. CF573D-Bear and Cavalry【动态dp】

    正题 题目链接:https://www.luogu.com.cn/problem/CF573D 题目大意 给出\(n\)个人\(n\)匹马,每个人/马有能力值\(w_i\)/\(h_i\). 第\(i ...

  6. vue-devtools 安装

    vue火了很久了,但是一直赶不上时代步伐的我今天才开始学,首先,根据vue官网介绍,推荐安装Vue Devtools.它允许你在一个更友好的界面中审查和调试 Vue 应用. 首先,将vue-devto ...

  7. Serverless 工程实践 | 零基础上手 Knative 应用

    作者|刘宇 前言:Knative 是一款基于 Kubernetes 的 Serverless 框架.其目标是制定云原生.跨平台的 Serverless 编排标准. Knative 介绍 Knative ...

  8. 题解 「BZOJ4919 Lydsy1706月赛」大根堆

    题目传送门 题目大意 给出一个 \(n\) 个点的树,每个点有权值,从中选出一些点,使得满足大根堆的性质.(即一个点的祖先节点如果选了那么该点的祖先节点的权值一定需要大于该点权值) 问能选出来的大根堆 ...

  9. 掌握BeanShell,轻松处理jmeter中的数据

    作者:季沐测试笔记 原文地址:https://www.cnblogs.com/testero/p/15424558.html 博客主页:https://www.cnblogs.com/testero ...

  10. Java(44)JDK新特性之函数式接口

    作者:季沐测试笔记 原文地址:https://www.cnblogs.com/testero/p/15201667.html 博客主页:https://www.cnblogs.com/testero ...