Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 75227bf

Browse files
committed
added task #1089 soluition
1 parent a83f577 commit 75227bf

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/task_1089/Solution.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package task_1089;
2+
3+
import java.util.Arrays;
4+
5+
class Solution {
6+
public void duplicateZeros(int[] arr) {
7+
int[] tmp = Arrays.copyOf(arr, arr.length);
8+
int tmpIndex = 0;
9+
for (int i = 0; i < tmp.length; i++) {
10+
if (tmp[i] != 0) {
11+
if (tmpIndex <= arr.length - 1) {
12+
arr[tmpIndex] = tmp[i];
13+
tmpIndex++;
14+
}
15+
} else {
16+
if (tmpIndex <= arr.length - 2) {
17+
arr[tmpIndex] = tmp[i];
18+
arr[tmpIndex+1] = tmp[i];
19+
tmpIndex += 2;
20+
} else if (tmpIndex <= arr.length - 1) {
21+
arr[tmpIndex] = tmp[i];
22+
tmpIndex++;
23+
}
24+
}
25+
}
26+
}
27+
28+
}

0 commit comments

Comments
 (0)