当前位置:网站首页>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--;
}
}
}
边栏推荐
- Life cycle of Servlet
- Redux - learning notes
- excel一小时不如JNPF表单3分钟,这样做报表,领导都得点赞!
- 低代码前景可期,JNPF灵活易用,用智能定义新型办公模式
- Find the combination number acwing 886 Find the combination number II
- I made mistakes that junior programmers all over the world would make, and I also made mistakes that I shouldn't have made
- 我們有個共同的名字,XX工
- 数位统计DP AcWing 338. 计数问题
- [rust notes] 05 error handling
- 低代码起势,这款信息管理系统开发神器,你值得拥有!
猜你喜欢

Monotonic stack -84 The largest rectangle in the histogram

Alibaba canal actual combat

DOM 渲染系统(render mount patch)响应式系统

Mortgage Calculator
![[concurrent programming] thread foundation and sharing between threads](/img/26/60fbfe65b186867a3b1cb58d481226.jpg)
[concurrent programming] thread foundation and sharing between threads

Find the combination number acwing 886 Find the combination number II

Binary tree traversal (first order traversal. Output results according to first order, middle order, and last order)

20220630学习打卡

On the setting of global variable position in C language

LeetCode 30. 串联所有单词的子串
随机推荐
20220630学习打卡
Life cycle of Servlet
第一个Servlet
Notes and bugs generated during the use of h:i:s and y-m-d
Solution of 300ms delay of mobile phone
[concurrent programming] collaboration between threads
Really explain the five data structures of redis
Debug debugging - Visual Studio 2022
Method of intercepting string in shell
Noip 2002 popularity group selection number
Allocation exception Servlet
22-05-26 西安 面试题(01)准备
Introduction to the usage of getopts in shell
状态压缩DP AcWing 91. 最短Hamilton路径
Using variables in sed command
注解简化配置与启动时加载
数字化管理中台+低代码,JNPF开启企业数字化转型的新引擎
Summary of methods for counting the number of file lines in shell scripts
cres
[concurrent programming] explicit lock and AQS