当前位置:网站首页>Longest continuous sequence [diffusion method + space for time]
Longest continuous sequence [diffusion method + space for time]
2022-06-25 01:37:00 【REN_ Linsen】
Diffusion method + Space for time
Preface
The core of space for time is to reduce the time complexity , But sometimes it's the key to solving problems , After all, time is limited ( First, double pointer ).
A typical space for time exchange is an array / Array hash/hash.
In this paper, we use the longest continuous sequence to experience space changing time , And practice the diffusion method .
One 、 The longest continuous sequence

Two 、 Diffusion method + Space for time
package everyday.medium;
import java.util.HashSet;
import java.util.Set;
// The longest continuous sequence
public class LongestConsecutive {
/* target: Find the longest consecutive number inside , If you order , direct O(n) Go through it and record the longest time . But the time complexity requires O(n), So we can only exchange space for time , First use hash Save up , It is convenient for quick search in the back ( Find by diffusion method ). */
public int longestConsecutive(int[] nums) {
// A special case , Special treatment .
if (0 == nums.length) return 0;
Set<Integer> set = new HashSet<>();
// preparation , Record all elements , And the initial value is 1, Indicates the number of consecutive numbers 1.
for (int n : nums) if (!set.contains(n)) set.add(n);
// Traverse nums, Find the total continuity of adjacent elements .
int ans = 1;
// Diffusion method , Each value can be accessed up to two times , Once there was , Once none .
for (int n : nums) {
// Unused numbers , From both sides .
if (set.contains(n)) {
// Start spreading on both sides , Diffusion method .
int len = 1;
int left = n, right = n;
while (set.contains(--left)) {
++len;
set.remove(left);
}
while (set.contains(++right)) {
++len;
set.remove(right);
}
// Take the maximum length of each diffusion .
ans = Math.max(ans, len);
}
}
return ans;
}
}
summary
1) Space for time
2) Diffusion method
reference
边栏推荐
- 实验5 8254定时/计数器应用实验【微机原理】【实验】
- Bi-sql Union
- Deoxyribonuclease I instructions in Chinese and English
- Unity C# 网络学习(六)——FTP(一)
- 百度语音合成语音文件并在网站中展示
- Unity C# 网络学习(六)——FTP(二)
- Listen to the markdown file and hot update next JS page
- "One good programmer is worth five ordinary programmers!"
- mpls 笔记 part 1
- Q1季度逆势增长的华为笔电,正引领PC进入“智慧办公”时代
猜你喜欢
![最长连续序列[扩散法+空间换时间]](/img/db/7b0d1b0db7015e887340723505153a.png)
最长连续序列[扩散法+空间换时间]

How to prepare for the last day of tomorrow's exam? Complete compilation of the introduction to the second building test site

Hands on data analysis data modeling and model evaluation

“一个优秀程序员可抵五个普通程序员!”

Numerical scheme simulation of forward stochastic differential equations with Markov Switching
![[leetcode] 11. Container with the most water](/img/40/8bb6506a29f8da797432fee50d3aad.png)
[leetcode] 11. Container with the most water

Abnova丨BSG 单克隆抗体中英文说明

Baidu voice synthesizes voice files and displays them on the website

pbcms添加循环数字标签

明日考试 最后一天如何备考?二造考点攻略全整理
随机推荐
RedisTemplate操作Redis,这一篇文章就够了(一)[通俗易懂]
Use redis' sorted set to make weekly hot Reviews
String common methods
Powerbi - for you who are learning
广发期货安全吗?开户需要什么东西?
Cloud development technology summit · public welfare programming challenge [hot registration]!
Tencent has completed the comprehensive cloud launch to build the largest cloud native practice in China
动手学数据分析 数据建模和模型评估
Abnova 5-methylcytosine polyclonal antibody
多模态数据也能进行MAE?伯克利&谷歌提出M3AE,在图像和文本数据上进行MAE!最优掩蔽率可达75%,显著高于BERT的15%
Tencent cloud wecity Industry joint collaborative innovation to celebrate the New Year!
脱氧核糖核酸酶I中英文说明书
Bi-sql delete
Assembly language (3) 16 bit assembly basic framework and addition and subtraction loop
leetcode:2104. 子数组范围和
IPC机制
Baidu voice synthesizes voice files and displays them on the website
海河实验室创新联合体成立 GBASE成为首批创新联合体(信创)成员单位
Experiment 5 8254 timing / counter application experiment [microcomputer principle] [experiment]
15. several methods of thread synchronization