当前位置:网站首页>LeetCode 1089. Duplicate zero
LeetCode 1089. Duplicate zero
2022-07-03 09:01:00 【Sasakihaise_】
【 queue 】 Didn't think of a good way , So we can only temporarily store the data in the queue to simulate
1. If the current number is not 0, Directly enter the queue
2. If the current number is 0, Take two. 0 In the queue
3. Modify the current position to the head of the queue
class Solution {
public void duplicateZeros(int[] arr) {
Queue<Integer> queue = new LinkedList();
for(var i = 0; i < arr.length; i++){
if(arr[i] != 0) queue.offer(arr[i]);
else {
queue.offer(0); queue.offer(0);
}
arr[i] = queue.poll();
}
}
}【 Double pointer 】 In fact, there is no need to queue , We can use double pointers to see the number of numbers we really need to go through the size of the original array , Then modify the array from back to front .
Take up a :1,0,2,3,0,4,5,0
Set double pointer i,j When opening, it points to 0,j For the faster pointer
1.i = j = 0,nums[i] != 0,i and j Go back one
2.i = j = 1, nums[i] == 0, i Go back to 1,j Go back to 2
until j Go to the n-1, here i The position of is the limit of the numerical value that the modified array can reach .
【 details 】0,1,0,0 This situation , Should be 0,0,1,0, But we found that i The pointer goes to the third 0 The location of , and j Already exceeded 4, So in this special case, we need to make an extra judgment .
class Solution {
// Double pointer 9:44 10
public void duplicateZeros(int[] arr) {
int i = 0, j = 0, flag = 0;
while(j < arr.length){
if(arr[i] == 0){
j++;
if(j == arr.length) flag = 1;
else j++;
}
else j++;
i++;
}
i--;
for(j = arr.length - 1; j >= 0;){
if(arr[i] == 0){
if(flag == 1){
arr[j--] = 0; flag = 0;
}else{
arr[j--] = 0; arr[j--] = 0;
}
}else{
arr[j--] = arr[i];
}
i--;
}
}
}
边栏推荐
- 22-06-27 西安 redis(01) 安装redis、redis5种常见数据类型的命令
- Find the combination number acwing 886 Find the combination number II
- Shell script kills the process according to the port number
- 第一个Servlet
- [rust note] 10 operator overloading
- 传统办公模式的“助推器”,搭建OA办公系统,原来就这么简单!
- [set theory] order relation (total order relation | total order set | total order relation example | quasi order relation | quasi order relation theorem | bifurcation | quasi linear order relation | q
- 常见渗透测试靶场
- Monotonic stack -84 The largest rectangle in the histogram
- DOM render mount patch responsive system
猜你喜欢

On a un nom en commun, maître XX.

LeetCode 324. 摆动排序 II

JS ternary operator - learning notes (with cases)

LeetCode 508. 出现次数最多的子树元素和

Memory search acwing 901 skiing

Servlet的生命周期

Drawing maze EasyX library with recursive backtracking method

Log4j2 vulnerability recurrence and analysis

Dom4j遍历和更新XML

Tree DP acwing 285 A dance without a boss
随机推荐
22-05-26 西安 面试题(01)准备
createjs easeljs
传统企业数字化转型需要经过哪几个阶段?
浅谈企业信息化建设
Using variables in sed command
LeetCode 1089. 复写零
22-06-27 Xian redis (01) commands for installing five common data types: redis and redis
[concurrent programming] explicit lock and AQS
Gif remove blank frame frame number adjustment
求组合数 AcWing 885. 求组合数 I
Binary tree sorting (C language, int type)
Facial expression recognition based on pytorch convolution -- graduation project
低代码起势,这款信息管理系统开发神器,你值得拥有!
[rust notes] 02 ownership
樹形DP AcWing 285. 沒有上司的舞會
[rust notes] 08 enumeration and mode
网络安全必会的基础知识
PHP uses foreach to get a value in a two-dimensional associative array (with instances)
The method of replacing the newline character '\n' of a file with a space in the shell
Gaussian elimination acwing 883 Gauss elimination for solving linear equations