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

Commit 5ba9110

Browse files
1089: Duplicate Zeros
1 parent 33db3be commit 5ba9110

File tree

4 files changed

+79
-0
lines changed

4 files changed

+79
-0
lines changed

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ My solutions to LeetCode problems in Kotlin.
2828
| [796](https://leetcode.com/problems/rotate-string/) | [Rotate String](src/main/kotlin/com/schmoczer/leetcode/_0796/RotateString.kt) | Easy |
2929
| [876](https://leetcode.com/problems/middle-of-the-linked-list/) | [Middle of the Linked List](src/main/kotlin/com/schmoczer/leetcode/_0876/MiddleOfTheLinkedList.kt) | Easy |
3030
| [977](https://leetcode.com/problems/squares-of-a-sorted-array/) | [Squares of a Sorted Array](src/main/kotlin/com/schmoczer/leetcode/_0977/SquaresOfASortedArray.kt) | Easy |
31+
| [1089](https://leetcode.com/problems/duplicate-zeros/) | [Duplicate Zeros](src/main/kotlin/com/schmoczer/leetcode/_1089/DuplicateZeros.kt) | Easy |
3132
| [1295](https://leetcode.com/problems/find-numbers-with-even-number-of-digits/) | [Find Numbers with Even Number of Digits](src/main/kotlin/com/schmoczer/leetcode/_1295/FindNumbersWithEvenNumberOfDigits.kt) | Easy |
3233
| [1342](https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/) | [Number of Steps to Reduce a Number to Zero](src/main/kotlin/com/schmoczer/leetcode/_1342/NumberOfStepsToReduceANumberToZero.kt) | Easy |
3334
| [1480](https://leetcode.com/problems/running-sum-of-1d-array/) | [Running Sum of 1d Array](src/main/kotlin/com/schmoczer/leetcode/_1480/RunningSumOf1dArray.kt) | Easy |
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.schmoczer.leetcode._1089
2+
3+
class DuplicateZeros {
4+
// Runtime 0ms Beats 100.00%
5+
fun duplicateZeros(arr: IntArray) {
6+
val size = arr.size
7+
val duplicate = IntArray(size)
8+
System.arraycopy(arr, 0, duplicate, 0, size)
9+
var index = 0
10+
for (number in duplicate) {
11+
if (index >= size) {
12+
break
13+
}
14+
arr[index] = number
15+
index++
16+
if (number == 0 && index < size) {
17+
arr[index] = number
18+
index++
19+
}
20+
}
21+
}
22+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Duplicate Zeros
2+
3+
Given a fixed-length integer array `arr`, duplicate each occurrence of zero, shifting the remaining elements to the
4+
right.
5+
6+
Note that elements beyond the length of the original array are not written. Do the above modifications to the input
7+
array in place and do not return anything.
8+
9+
Example 1:
10+
11+
> Input: arr = [1,0,2,3,0,4,5,0]
12+
>
13+
> Output: [1,0,0,2,3,0,0,4]
14+
15+
Example 2:
16+
17+
> Input: arr = [1,2,3]
18+
>
19+
> Output: [1,2,3]
20+
21+
Constraints:
22+
23+
- `1 <= arr.length <= 10^4`
24+
- `0 <= arr[i] <= 9`
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.schmoczer.leetcode._1089
2+
3+
import org.junit.jupiter.api.BeforeEach
4+
import org.junit.jupiter.params.ParameterizedTest
5+
import org.junit.jupiter.params.provider.Arguments
6+
import org.junit.jupiter.params.provider.MethodSource
7+
import kotlin.test.assertContentEquals
8+
9+
class DuplicateZerosTest {
10+
private companion object {
11+
@JvmStatic
12+
fun inputAndDuplicatedZeros() = listOf(
13+
Arguments.of(intArrayOf(1, 0, 2, 3, 0, 4, 5, 0), intArrayOf(1, 0, 0, 2, 3, 0, 0, 4)),
14+
Arguments.of(intArrayOf(1, 2, 3), intArrayOf(1, 2, 3)),
15+
)
16+
}
17+
18+
private lateinit var sut: DuplicateZeros
19+
20+
@BeforeEach
21+
fun setUp() {
22+
sut = DuplicateZeros()
23+
}
24+
25+
@ParameterizedTest(name = "{0} with duplicate zeros is {1}")
26+
@MethodSource("inputAndDuplicatedZeros")
27+
fun `input array is modified in place to duplicate all zeroes`(input: IntArray, expected: IntArray) {
28+
sut.duplicateZeros(input)
29+
30+
assertContentEquals(expected, input)
31+
}
32+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /