-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDuplicateZeros.java
More file actions
48 lines (46 loc) · 1.1 KB
/
DuplicateZeros.java
File metadata and controls
48 lines (46 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<<<<<<< HEAD
//1089. Duplicate Zeros
class Solution {
public void duplicateZeros(int[] arr) {
int n = arr.length;
int zero = 0;
for(int num : arr) if(num == 0) zero++;
int i = n - 1;
int j = n + zero - 1;
while(i >= 0){
if(arr[i] != 0){
if(j < n) arr[j] = arr[i];
}
else{
if(j < n) arr[j] = 0;
j--;
if(j < n) arr[j] = 0;
}
i--;
j--;
}
}
=======
//1089. Duplicate Zeros
class Solution {
public void duplicateZeros(int[] arr) {
int n = arr.length;
int zero = 0;
for(int num : arr) if(num == 0) zero++;
int i = n - 1;
int j = n + zero - 1;
while(i >= 0){
if(arr[i] != 0){
if(j < n) arr[j] = arr[i];
}
else{
if(j < n) arr[j] = 0;
j--;
if(j < n) arr[j] = 0;
}
i--;
j--;
}
}
>>>>>>> be6ce0b427078b1421d5dd74adb2300dc02daeec
}