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. request,response统一编码

    方法:统一使用编码(例如UTF-8编码)解决session或jsp等各种值传递时的中文乱码问题 request.setCharacterEncoding("UTF-8"); res ...

  2. git介绍-常用操作(一)

    Table of Contents 1  系列文章 2  git说明 3  git常用命令 3.1  基本操作 3.2  远程操作 4  查看git的配置 4.1  查看已配置项 4.2  其他配置 ...

  3. ecshop transport.js IE报错(608行),对象不支持此属性或方法 的解决办法

    解决办法: 将if (this.hasOwnProperty(k)) { 改为: if (this.hasOwnProperty && this.hasOwnProperty(k)) ...

  4. 用 shell 脚本做命令行工具扩展

    问题的提出 公司开发机与远程服务器之间有严格的隔离策略,不能直接使用 ssh 登录,而必需通过跳板机.这样一来,本地与服务器之间的一些文件传输变得非常不便.经过咨询,运维教了我一招: $ nc -l ...

  5. Jmeter系列(10)- Linux环境安装之Jmeter下载配置

    step-1下载 我是之前windows有,就直接copy到Linux系统了 step-2Jmter放到local目录 mv apache-jmeter-5.2.1 /usr/local/ step- ...

  6. Linux系列(37) - 源码包与RPM包区别(1)

    源码包是不能使用[service]命令来启动服务,因为源码包的安装位置由用户指定 源码包一般安装在: /usr/local/软件名/ ,源码包安装的服务,只能用绝对路径进行服务的管理 rpm包安装后, ...

  7. Jmeter目录分析

    通讯协议 最重要的基础知识. 性能测试的原理?通过协议模拟压力 jmeter目录结果解析 bin docs,文档 lib,存放库文件,所依赖的jar包 ext jmeter自己的jar包 jmeter ...

  8. 防刷功能的实现(thinkphp5)

    $seconds = '3'; //时间段[秒] $refresh = '3';//最大次数 $cur_time = time(); if(Session::get('refresh_times')) ...

  9. [转载]CentOS 下安装LEMP服务(Nginx、MariaDB/MySQL和PHP)

    LEMP 组合包是一款日益流行的网站服务组合软件包,在许多生产环境中的核心网站服务上起着强有力的作用.正如其名称所暗示的, LEMP 包是由 Linux.nginx.MariaDB/MySQL 和 P ...

  10. bzoj#2407-探险【最短路,二进制分组】

    正题 题目链接:https://darkbzoj.tk/problem/2407 题目大意 \(n\)个点的一张无向图(但是正反权值不同),求一个从\(1\)出发回到\(1\)且不经过重复边的最短路径 ...