-
Notifications
You must be signed in to change notification settings - Fork 1
/
DuplicateZeros.java
38 lines (32 loc) · 1013 Bytes
/
DuplicateZeros.java
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
package com.smlnskgmail.jaman.leetcodejava.easy;
// https://leetcode.com/problems/duplicate-zeros/
public class DuplicateZeros {
private final int[] input;
public DuplicateZeros(int[] input) {
this.input = input;
}
public void solution() {
int possibleDups = 0;
int length = input.length - 1;
for (int left = 0; left <= length - possibleDups; left++) {
if (input[left] == 0) {
if (left == length - possibleDups) {
input[length] = 0;
length -= 1;
break;
}
possibleDups++;
}
}
int last = length - possibleDups;
for (int i = last; i >= 0; i--) {
if (input[i] == 0) {
input[i + possibleDups] = 0;
possibleDups--;
input[i + possibleDups] = 0;
} else {
input[i + possibleDups] = input[i];
}
}
}
}