Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit55e7fe3

Browse files
add 1089
1 parent909762f commit55e7fe3

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+
packagecom.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+
publicclass_1089 {
28+
publicstaticclassSolution1 {
29+
publicvoidduplicateZeros(int[]arr) {
30+
for (inti =0;i <arr.length -1;i++) {
31+
if (arr[i] ==0) {
32+
duplicateZeros(arr,i +1);
33+
i++;
34+
}
35+
}
36+
}
37+
38+
privatevoidduplicateZeros(int[]arr,intzeroIndex) {
39+
inttmp =arr[zeroIndex];
40+
arr[zeroIndex] =0;
41+
for (inti =zeroIndex +1;i <arr.length;i++) {
42+
intnext =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+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.solutions._1089;
4+
importorg.junit.BeforeClass;
5+
importorg.junit.Test;
6+
7+
importstaticorg.junit.Assert.assertArrayEquals;
8+
9+
publicclass_1089Test {
10+
privatestatic_1089.Solution1solution1;
11+
privatestaticint[]arr;
12+
13+
@BeforeClass
14+
publicstaticvoidsetup() {
15+
solution1 =new_1089.Solution1();
16+
}
17+
18+
@Test
19+
publicvoidtest1() {
20+
arr =newint[]{1,0,2,3,0,4,5,0};
21+
solution1.duplicateZeros(arr);
22+
assertArrayEquals(newint[]{1,0,0,2,3,0,0,4},arr);
23+
}
24+
25+
@Test
26+
publicvoidtest2() {
27+
arr =newint[]{1,2,3};
28+
solution1.duplicateZeros(arr);
29+
assertArrayEquals(newint[]{1,2,3},arr);
30+
}
31+
32+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp