longest-consecutive-sequence leetcode C++
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++的更多相关文章
- 128. Longest Consecutive Sequence(leetcode)
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- Binary Tree Longest Consecutive Sequence -- LeetCode
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- Longest Consecutive Sequence [LeetCode]
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- Longest Consecutive Sequence——Leetcode
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- Longest Consecutive Sequence leetcode java
题目: Given an unsorted array of integers, find the length of the longest consecutive elements sequenc ...
- [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 ...
- LeetCode Binary Tree Longest Consecutive Sequence
原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/ 题目: Given a binary t ...
- 【LeetCode OJ】Longest Consecutive Sequence
Problem Link: http://oj.leetcode.com/problems/longest-consecutive-sequence/ This problem is a classi ...
- [LeetCode] 128. Longest Consecutive Sequence 解题思路
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
随机推荐
- 记一次docker compose的低级错误
记一次docker compose的低级错误 问题 今天在学习dockercompose的时候,启动docker compose up,结果却出现异常 Error response from da ...
- fontawesome图标不显示的原因
1.查看css路径是否正确 2.查看font文件夹内的字体文件是否引入 3.查看font文件夹内的字体资源路径是否正确
- Jmeter系列(2)- 代理服务器录制脚本
操作步骤 step-1 添加代理服务器 step-2 添加线程组 step-3 添加录制控制器 HTTP代理服务器配置 - HTTP(S) Test Script Recorder TestPlan ...
- 『Python』matplotlib坐标轴应用
1. 设置坐标轴的位置和展示形式 import numpy as np import matplotlib.pyplot as plt import matplotlib as mpl mpl.use ...
- Java-Ide快速创建getter&setter方法
1.右键 选择Generate 2.创建 类的equals方法
- Ubuntu开发相关环境搭建
一.Ubuntu系统语言环境切换修改 安装时,选择的中文版,但实际使用起来,很不爽,果断切换为英文 1.1 打开终端: vim /etc/default/locale 1.2 修改配置 LANG=&q ...
- 开源框架 - 新 代码生成器 WebFirst / .NET Core
框架描述 WebFirst 是一新代的 代码生成器,用法简单,功能强大,支持多种数据库 ,具体功能如下: 一. 建库.CodeFirst方式在线建表,没用到CodeFirst的用户可以用工具轻松体验 ...
- 怒肝 Linux 学习路线,这回不难
Linux 学习路线 by 鱼皮. 原创不易,请勿抄袭,违者必究! 大家好,我是鱼皮,又花 1 周肝出了 Linux 学习资料全家桶,包括学习路线.命令手册.视频.书籍.文档.实战教程.社区.工具.大 ...
- JUC之Executor,ExecutorService接口,AbstractExecutorService类
java多线程的Executor中定义了一个execut方法,ExecutorService接口继承了Executor接口,并进行了功能的扩展组合,定义了shutdown,shutdownNow,su ...
- Java(42)JDK新特性之接口组成
作者:季沐测试笔记 原文地址:https://www.cnblogs.com/testero/p/15201666.html 博客主页:https://www.cnblogs.com/testero ...