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

Commit 55e7fe3

Browse files
add 1089
1 parent 909762f commit 55e7fe3

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Your ideas/fixes/algorithms are more than welcome!
3131
|5083|[Occurrences After Bigram](https://leetcode.com/problems/occurrences-after-bigram/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_5083.java) | O(n) | O(1) | |Easy||
3232
|1137|[N-th Tribonacci Number](https://leetcode.com/problems/n-th-tribonacci-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1137.java) | O(n) | O(n) | |Easy||
3333
|1122|[Relative Sort Array](https://leetcode.com/problems/relative-sort-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1122.java) | O(n) | O(n) | |Easy||
34+
|1089|[Duplicate Zeros](https://leetcode.com/problems/duplicate-zeros/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1089.java) | O(n^2) | O(1) | |Easy||
3435
|1071|[Greatest Common Divisor of Strings](https://leetcode.com/problems/greatest-common-divisor-of-strings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1071.java) | O(m*n) | O(1) | |Easy||
3536
|1065|[Index Pairs of a String](https://leetcode.com/problems/index-pairs-of-a-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1065.java) | O(nlogn) | O(1) | |Medium||
3637
|1056|[Confusing Number](https://leetcode.com/problems/confusing-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1056.java) | O(n) | O(1) | |Easy||
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 1089. Duplicate Zeros
5+
*
6+
* Given a fixed length array arr of integers, duplicate each occurrence of zero, shifting the remaining elements to the right.
7+
*
8+
* Note that elements beyond the length of the original array are not written.
9+
*
10+
* Do the above modifications to the input array in place, do not return anything from your function.
11+
*
12+
* Example 1:
13+
* Input: [1,0,2,3,0,4,5,0]
14+
* Output: null
15+
* Explanation: After calling your function, the input array is modified to: [1,0,0,2,3,0,0,4]
16+
*
17+
* Example 2:
18+
* Input: [1,2,3]
19+
* Output: null
20+
* Explanation: After calling your function, the input array is modified to: [1,2,3]
21+
*
22+
* Note:
23+
*
24+
* 1 <= arr.length <= 10000
25+
* 0 <= arr[i] <= 9
26+
* */
27+
public class _1089 {
28+
public static class Solution1 {
29+
public void duplicateZeros(int[] arr) {
30+
for (int i = 0; i < arr.length - 1; i++) {
31+
if (arr[i] == 0) {
32+
duplicateZeros(arr, i + 1);
33+
i++;
34+
}
35+
}
36+
}
37+
38+
private void duplicateZeros(int[] arr, int zeroIndex) {
39+
int tmp = arr[zeroIndex];
40+
arr[zeroIndex] = 0;
41+
for (int i = zeroIndex + 1; i < arr.length; i++) {
42+
int next = arr[i];
43+
arr[i] = tmp;
44+
tmp = next;
45+
}
46+
}
47+
}
48+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1089;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertArrayEquals;
8+
9+
public class _1089Test {
10+
private static _1089.Solution1 solution1;
11+
private static int[] arr;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _1089.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
arr = new int[]{1, 0, 2, 3, 0, 4, 5, 0};
21+
solution1.duplicateZeros(arr);
22+
assertArrayEquals(new int[]{1, 0, 0, 2, 3, 0, 0, 4}, arr);
23+
}
24+
25+
@Test
26+
public void test2() {
27+
arr = new int[]{1, 2, 3};
28+
solution1.duplicateZeros(arr);
29+
assertArrayEquals(new int[]{1, 2, 3}, arr);
30+
}
31+
32+
}

0 commit comments

Comments
 (0)