博客
关于我
整数序列中最长的连续序列个数(LeetCode-128)
阅读量:344 次
发布时间:2019-03-04

本文共 541 字,大约阅读时间需要 1 分钟。

1、题目描述

    求一个整数序列中最长的连续子序列个数,比如输入:[100, 4, 200, 1, 3, 2],输出:4

2、解题思路

(1)先排序,从有序序列中找连续子序列,时间复杂度为O(nlogn),代码如下:

int longestConsecutive(vector
& nums) { if(nums.size()==0) return 0; sort(nums.begin(),nums.end()); int count = 1; int len = 1; int j = 1; for(int i=1;i

(2)使用哈希表(unordered_set)记录每个元素是否出现,然后查找,时间复杂度为O(n),空间复杂度为O(n),代码如下:

int longestConsecutive(vector
& nums) { if(nums.size()==0) return 0; unordered_set
temp; for(int i=0;i

 

转载地址:http://tbqe.baihongyu.com/

你可能感兴趣的文章
node~ http缓存
查看>>
node不是内部命令时配置node环境变量
查看>>
node中fs模块之文件操作
查看>>
Node中同步与异步的方式读取文件
查看>>
node中的get请求和post请求的不同操作【node学习第五篇】
查看>>
Node中的Http模块和Url模块的使用
查看>>
Node中自启动工具supervisor的使用
查看>>
Node入门之创建第一个HelloNode
查看>>
node全局对象 文件系统
查看>>
Node出错导致运行崩溃的解决方案
查看>>
Node响应中文时解决乱码问题
查看>>
node基础(二)_模块以及处理乱码问题
查看>>
node安装卸载linux,Linux运维知识之linux 卸载安装node npm
查看>>
node安装及配置之windows版
查看>>
Node实现小爬虫
查看>>
Node提示:error code Z_BUF_ERROR,error error -5,error zlib:unexpected end of file
查看>>
Node提示:npm does not support Node.js v12.16.3
查看>>