当前位置:网站首页>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--;
}
}
}
边栏推荐
- [concurrent programming] collaboration between threads
- Markdown learning
- Get the link behind? Parameter value after question mark
- 数字化管理中台+低代码,JNPF开启企业数字化转型的新引擎
- Arbre DP acwing 285. Un bal sans patron.
- String splicing method in shell
- Gaussian elimination acwing 883 Gauss elimination for solving linear equations
- file_ put_ contents
- 低代码前景可期,JNPF灵活易用,用智能定义新型办公模式
- Phpstudy 80 port occupied W10 system
猜你喜欢
PHP uses foreach to get a value in a two-dimensional associative array (with instances)
樹形DP AcWing 285. 沒有上司的舞會
[concurrent programming] concurrent tool class of thread
Slice and index of array with data type
传统企业数字化转型需要经过哪几个阶段?
Complex character + number pyramid
状态压缩DP AcWing 291. 蒙德里安的梦想
First Servlet
Concurrent programming (VI) ABA problems and solutions under CAS
一个优秀速开发框架是什么样的?
随机推荐
Methods of checking ports according to processes and checking processes according to ports
ES6 promise learning notes
22-05-26 Xi'an interview question (01) preparation
[concurrent programming] concurrent security
剑指 Offer II 091. 粉刷房子
The method for win10 system to enter the control panel is as follows:
网络安全必会的基础知识
22-06-28 西安 redis(02) 持久化机制、入门使用、事务控制、主从复制机制
传统企业数字化转型需要经过哪几个阶段?
即时通讯IM,是时代进步的逆流?看看JNPF怎么说
Six dimensional space (C language)
PHP mnemonic code full text 400 words to extract the first letter of each Chinese character
On the setting of global variable position in C language
LeetCode 30. 串联所有单词的子串
JS ternary operator - learning notes (with cases)
AcWing 787. 归并排序(模板)
Dom4j traverses and updates XML
树形DP AcWing 285. 没有上司的舞会
[concurrent programming] explicit lock and AQS
Binary tree sorting (C language, int type)