【LeetCode-128】Longest Consecutive Sequence
描述
输入一个乱序的连续数列,输出其中最长连续数列长度,要求算法复杂度为 O(n) 。
输入
54,55,300,12,56
输出
3
通常我们看有没有连续序列时 找某个数有没有的前后的数,比如看到55,我们会下意识找56,57...54,53...这道题做法也是这样。
我的做法是用map映射,键为列表中的每个数,值为1。然后遍历数组,比如数为55,找这个数的前后有没有map映射后值为1的,有的话sum++,为了防止重复遍历需要把遍历过的数的映射值改成-1。
class Solution
{
public:
int longestConsecutive(vector<int>& nums)
{
map<int, int>M;
int len = nums.size(), ans = ;
for(int i = ; i < len; i++)
M[nums[i]] = ;
for(int i = ; i < len; i++)
{
if(M[nums[i]] == -) continue;
int sum = , j = nums[i];
while()
{
M[j] = -;
if(M[++j] == )
{
sum++;
continue;
}
break;
}
j = nums[i];
while()
{
M[j] = -;
if(M[--j] == )
{
sum ++;
continue;
}
break;
}
ans = max(ans, sum);
}
return ans;
}
};
【LeetCode-128】Longest Consecutive Sequence的更多相关文章
- 【LeetCode OJ】Longest Consecutive Sequence
Problem Link: http://oj.leetcode.com/problems/longest-consecutive-sequence/ This problem is a classi ...
- 【leetcode】Longest Consecutive Sequence
Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...
- [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 Binary Tree Longest Consecutive Sequence
原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/ 题目: Given a binary t ...
- [LeetCode] Binary Tree Longest Consecutive Sequence II 二叉树最长连续序列之二
Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree. Especia ...
- 【leetcode】Longest Consecutive Sequence(hard)☆
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- 【leetcode刷题笔记】Longest Consecutive Sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- LeetCode(128) Longest Consecutive Sequence
题目 Given an unsorted array of integers, find the length of the longest consecutive elements sequence ...
- 【LeetCode OJ】Longest Palindromic Substring
题目链接:https://leetcode.com/problems/longest-palindromic-substring/ 题目:Given a string S, find the long ...
随机推荐
- Java应用开发中的SQL注入攻击
1. 什么是SQL注入攻击? SQL注入攻击是黑客对数据库进行攻击的常用手段之一.随着B/S模式应用开发的发展,使用这种模式编写应用程序的程序员越来越多.但是由于程序员的水平及经验参差不齐,相当一部分 ...
- eclipse 创建jsp报错
- 本地多张图片采用jmeter上传到ftp服务器的方法和获取服务器日志中某些关键字的基本方法
测试需求: 本地图片上传到ftp服务器里和另外两台不同算法比对服务器进行比对,得出漏检和误检结果:这实际属于功能测试范畴. 测试思路: 第一种方法:使用实际场景的摄像机抓拍图片上传到服务器,用录屏软件 ...
- 一键安装lnmp(2)
all(){path=`pwd`cd $pathechoecho "exclude=*.i386 *.i686" >> /etc/yum.confrpm -ivh ht ...
- 20145215《Java程序设计》第二周学习总结
教材内容总结 类型.变量与运算符 *基本类型 整数(short.int.long) 字节(byte) 浮点数(float/double) 字符(char)将一个数字字母或者符号用单引号标识,字符串用双 ...
- 强大的jQuery选择器 平时用的太少了 下次要先来看看
- python-运算、分支、深浅拷贝
算术表达式: + - * / 除法Python3中是默认向浮点数靠拢 //取整运算 结果的最小整数靠拢 向下 5 // 2 = 2(向下取整) %取余运算 5 % 2 = 1 **幂值运算 ...
- [BZOJ3174]拯救小矮人
Description 一群小矮人掉进了一个很深的陷阱里,由于太矮爬不上来,于是他们决定搭一个人梯.即:一个小矮人站在另一小矮人的 肩膀上,知道最顶端的小矮人伸直胳膊可以碰到陷阱口.对于每一个小矮人, ...
- NAT模式下远程连接centos6虚拟机与虚拟机网络配置
最近装了centos,但是没有网络,也无法远程连接.关键是虚拟机中没有ip地址. 网上方法很多,但是每个人情况不一样,所以不尽适用. 1.解决这个问题,首先保证你的vmware的dhcp服务和net服 ...
- 虚拟机 Ubuntu18.04 tensorflow cpu 版本
虚拟机 Ubuntu18.04 tensorflow cpu 版本 虚拟机VMware 配置: 20G容量,可扩充 2G内存,可扩充 网络采用NAT模式 平台:win10下的Ubuntu18.04 出 ...