首页
题库
面试
求职
学习
竞赛
More+
所有博客
搜索面经/职位/试题/公司
搜索
我要招人
去企业版
登录 / 注册
首页
>
试题广场
>
最长的连续元素序列长度
[编程题]最长的连续元素序列长度
热度指数:29565
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32M,其他语言64M
算法知识视频讲解
给定一个无序的整数类型数组,求最长的连续
元素
序列的长度。
例如:
给出的数组为
[1000, 4, 2000, 1, 3, 2],
最长的连续
元素
序列
为
[1, 2, 3, 4].
返回这个序列的长度:4
你需要给出
时间
复杂度在O(n)之内的算法
示例1
输入
[1000, 4, 2000, 1, 3, 2]
输出
4
马上挑战
算法知识视频讲解
提交运行
算法知识视频讲解
添加笔记
求解答(16)
邀请回答
收藏(175)
分享
提交结果有问题?
113个回答
5篇题解
开通博客
华科不平凡
发表于 2020-09-23 18:05:08
采用哈希表存储每个元素,然后遍历整个数组,遍历的时候求当前元素所在连续序列的长度。 如[3, 1, 2, 8, 9],存到哈希表之后,我们遍历这个数组,首先遇到的是3,我们在哈希表中查找3之前的连续数,以及查找3之后的连续数,查完就从哈希表中删除(因为是连续的,所以删除不会影响最终结果),并且更新结
展开全文
卫宫士郎红A
发表于 2020-08-23 16:58:58
给定一个无序的整数类型数组,求最长的连续元素序列的长度。例如:给出的数组为[100, 4, 200, 1, 3, 2],最长的连续元素序列为[1, 2, 3, 4]. 返回这个序列的长度:4你需要给出时间复杂度在O(n)之内的算法 这道题我想到的有两种做法:1、用一个辅助数组,记录下来数组每个位置对
展开全文
北理星星
发表于 2020-08-16 10:44:10
class Solution { public: /** * * @param num int整型vector * @return int整型 */ int longestConsecutive(vector<int>&
展开全文
ButterFlyEffect
发表于 2020-11-08 17:46:01
本题第一时间想到的解法就是排序,但是这样不符合O(n)的时间复杂度要求。另外一种思路就是用一个set记录每个元素的位置,然后遍历数组的元素,对每个元素做向上向下的遍历,统计连续的长度。几个点:1 普通的set查找是O(logN)的,改成hash set就可以认为是O(1)的了。2 在之前序列统计过的
展开全文
君潇然
发表于 2023-05-21 12:48:38
class Solution { public: /** * * @param num int整型vector * @return int整型 */ int longestConsecutive(vector<int>& num
展开全文
问题信息
数组
查找
难度:
113条回答
175收藏
36556浏览
热门推荐
通过挑战的用户
查看代码
牛客38959...
2023-02-15 15:30:38
开心的孤勇者在抱佛脚
2023-02-14 10:32:50
牛客38441...
2022-09-13 16:23:41
牛客61312...
2022-09-10 23:41:54
牛客72674...
2022-09-09 15:32:57
相关试题
明明的随机数
数组
评论
(3692)
来自
华为研发工程师编程题
已知a
40
=...
京东
职能
2019
财务
保险
评论
(1)
《拳皇97》最后BOSS是谁?
游戏常识
评论
(1)
《魔兽世界》中,下列不属于玩家可以...
游戏常识
评论
(1)
有20000人的就餐需求,现建了一...
评论
(1)
扫描二维码,关注牛客网
意见反馈
下载牛客APP,随时随地刷题
import java.util.*; public class Solution { /** * * @param num int整型一维数组 * @return int整型 */ public int longestConsecutive (int[] num) { // write code here } }
class Solution { public: /** * * @param num int整型vector * @return int整型 */ int longestConsecutive(vector
& num) { // write code here } };
# # # @param num int整型一维数组 # @return int整型 # class Solution: def longestConsecutive(self , num ): # write code here
/** * * @param num int整型一维数组 * @return int整型 */ function longestConsecutive( num ) { // write code here } module.exports = { longestConsecutive : longestConsecutive };
# # # @param num int整型一维数组 # @return int整型 # class Solution: def longestConsecutive(self , num ): # write code here
package main /** * * @param num int整型一维数组 * @return int整型 */ func longestConsecutive( num []int ) int { // write code here }
[1000, 4, 2000, 1, 3, 2]
4