Repository navigation
Expand file tree
/
Copy pathDuplicateZeros.java
More file actions
52 lines (49 loc) · 1.55 KB
/
Copy pathDuplicateZeros.java
File metadata and controls
52 lines (49 loc) · 1.55 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
49
50
51
52
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
public class DuplicateZeros {
/**
* Given a fixed-length integer array arr, duplicate each occurrence of zero, shifting the remaining elements to the right.
* <p>
* Note that elements beyond the length of the original array are not written. Do the above modifications to the input array in place and do not return anything.
* <p>
* <p>
* <p>
* Example 1:
* <p>
* Input: arr = [1,0,2,3,0,4,5,0]
* Output: [1,0,0,2,3,0,0,4]
* Explanation: After calling your function, the input array is modified to: [1,0,0,2,3,0,0,4]
* <p>
* Example 2:
* <p>
* Input: arr = [1,2,3]
* Output: [1,2,3]
* Explanation: After calling your function, the input array is modified to: [1,2,3]
* <p>
* <p>
* <p>
* Constraints:
* <p>
* 1 <= arr.length <= 104
* 0 <= arr[i] <= 9
*/
public static void main(String[] args) {
int[] arr = {1, 0, 2, 3, 0, 4, 5, 0};
duplicateZeros(arr);
System.out.println(Arrays.toString(arr));
}
public static void duplicateZeros(int[] arr) {
Queue<Integer> stack = new LinkedList<>();
for (int i = 0; i < arr.length; i++) {
int temp = !stack.isEmpty() ? stack.remove() : Integer.MIN_VALUE;
if (arr[i] == 0) {
stack.add(arr[i]);
}
if (temp != Integer.MIN_VALUE) {
stack.add(arr[i]);
arr[i] = temp;
}
}
}
}