当前位置:网站首页>Leetcode-128-longest continuous sequence
Leetcode-128-longest continuous sequence
2022-07-18 09:28:00 【z754916067】
subject

Ideas
- Violent words arrays.sort Then you can find it directly , But such words seem to have O(nlogn) Time complexity of dissatisfaction O(n), Since it is O(n) The algorithm of , That's a traversal , It can't be dp,dp Well, that's right O(n^2)
- The solution of the problem is really simple , But I don't know why, but I didn't expect , Even if I think of it, I don't think it counts O(n)… Take note .
Code
public int longestConsecutive(int[] nums) {
//hashset Duplicate data storage is not allowed Direct filtration nums Data in
Set<Integer> num_set = new HashSet<Integer>();
// Join in
for(int num:nums) num_set.add(num);
// The answer returned
int ans=0;
for (int num : num_set) {
// For every number Find the former and the latter
if (!num_set.contains(num - 1)) {
// Be sure to find the smallest in the sequence Therefore, it is possible to become the smallest without the previous one
int currentNum = num;
// Maximum possible length
int currentStreak = 1;
// Move back
while (num_set.contains(currentNum + 1)) {
currentNum += 1;
currentStreak += 1;
}
// Just compare
ans = Math.max(ans, currentStreak);
}
}
return ans;
}
边栏推荐
- Help brand insight -- Analysis of consumers' emotional behavior
- Codeforces Round #802 B. Palindromic Numbers
- 真实案例,大学生接单被骗,希望大家不要被骗了【惨痛教训】
- 我国数据安全治理研究
- 面试复盘2(面试回答不好的地方做总结)
- 日志的管理与应急处理
- 微服务学习
- Scenarios that must be considered when designing a stable microservice system
- Codeforces Round #804 B Almost Ternary Matrix
- User login and registration function with verification code
猜你喜欢

迪赛智慧数——折(渐变堆叠图):全国居民人均可支配收入

Codeforces Round #806 (Div. 4) A - G

Leetcode-14-longest public prefix

Responsive dream weaving template home furniture building materials website

HCIP第八天笔记

AIRIOT答疑第4期|如何使用数据分析引擎?

怎么解决系统高并发问题的思路?看完你就彻底懂了。

Codeforces Round #802 D. River Locks

【空间&单细胞组学】第2期:联合单细胞和bulk转录组鉴定了结直肠癌中两种上皮肿瘤细胞状态,并完善了CMS分型

openGauss 联合产业界创新,共建开源数据库根社区
随机推荐
Leetcode-14-longest public prefix
基于角色的云环境下虚拟机安全访问控制策略
Codeforces Round #803 (Div. 2) B. Rising Sand
网络信息查看及配置
【ARCGIS创建中国南海诸岛及九段线小图框】
Unity Shader——CGInclude文件cginc
Exness: crude oil stopped falling and rebounded. Pay attention to the performance of US terrorist data in the evening
答读者问(7):关于doublet
Analysis of quantum secure communication
如何在dataworks写ADB的sql
SQL也能玩转AI ?没错!MLOps Meetup V3 回顾|OpenMLBD+SQLFlow+Byzer
Network address translation
Codeforces Round #802 D. River Locks
开机按F11 选择 one-shot再选U盘启动
构建数据基础制度,开启OID赛道新纪元
Timeline和Playable导致Unity崩溃问题记录
5个时间序列预测的深度学习模型对比总结:从模拟统计模型到可以预训练的无监督模型
Opengauss cooperates with industry innovation to build a root community of open source databases
Can SQL also play AI? you 're right! Mlops meetup V3 review openmlbd+sqlflow+byzer
Nest 框架