Skip to content

Commit ad64e79

Browse files
committed
Merge remote-tracking branch 'origin/master' into exercise/id977
2 parents 817719f + a33ede9 commit ad64e79

File tree

4 files changed

+122
-3
lines changed

4 files changed

+122
-3
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,13 @@ My Kotlin exercises with tests from [Leetcode](https://leetcode.com/kotlerdev)
4747

4848
#### Array
4949

50-
| ID | Description | Solution | Test | Difficulty |
51-
|:---:|:-------------------------------------------------------------------------------------------------|:-----------------------------------------------------------------:|:-----------------------------------------------------------------:|:----------:|
50+
| ID | Description | Solution | Test | Difficulty |
51+
|:---:|:----------------------------------------------------------------------------------------------|:-----------------------------------------------------------------:|:-----------------------------------------------------------------:|:----------:|
5252
| 1 | [Two Sum](src/main/kotlin/exercise/easy/id1/Description1.md) | [solution](src/main/kotlin/exercise/easy/id1/Solution1.kt) | [test](src/test/kotlin/exercise/easy/id1/Solution1Test.kt) | Easy |
5353
| 121 | [Best Time to Buy and Sell stock Easy](src/main/kotlin/exercise/easy/id121/Description121.md) | [solution](src/main/kotlin/exercise/easy/id121/Solution121.kt) | [test](src/test/kotlin/exercise/easy/id121/Solution121Test.kt) | Easy |
5454
| 169 | [Majority Element](src/main/kotlin/exercise/easy/id169/Description169.md) | [solution](src/main/kotlin/exercise/easy/id169/Solution169.kt) | [test](src/test/kotlin/exercise/easy/id169/Solution169Test.kt) | Easy |
55-
| 217 | [Contains Duplicate](src/main/kotlin/exercise/easy/id217/Description217.md)) | [solution](src/main/kotlin/exercise/easy/id217/Solution217.kt) | [test](src/test/kotlin/exercise/easy/id217/Solution217Test.kt) | Easy |
55+
| 217 | [Contains Duplicate](src/main/kotlin/exercise/easy/id217/Description217.md) | [solution](src/main/kotlin/exercise/easy/id217/Solution217.kt) | [test](src/test/kotlin/exercise/easy/id217/Solution217Test.kt) | Easy |
56+
| 283 | [Move Zeroes](src/main/kotlin/exercise/easy/id283/Description283.md) | [solution](src/main/kotlin/exercise/easy/id283/Solution283.kt) | [test](src/test/kotlin/exercise/easy/id283/Solution283Test.kt) | Easy |
5657

5758
[//]: # (https://www.techinterviewhandbook.org/grind75?weeks=26&hours=40&grouping=topics)
5859

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
[//]: # (Copyright [2023] [Anton Kotler kotler.developer@gmail.com] License MIT)
2+
3+
# Move Zeroes
4+
5+
Given an integer array `nums`, move all `0`'s to the end of it while maintaining the relative order of the non-zero
6+
elements.
7+
Note that you must do this in-place without making a copy of the array.
8+
9+
![Difficulty](https://img.shields.io/badge/Difficulty-Easy-61904f)
10+
11+
Example 1:
12+
13+
```
14+
Input: nums = [0,1,0,3,12]
15+
Output: [1,3,12,0,0]
16+
```
17+
18+
Example 2:
19+
20+
```
21+
Input: nums = [0]
22+
Output: [0]
23+
```
24+
25+
Example 3:
26+
27+
```
28+
Input: nums = [1,1,1,3,3,4,3,2,4,2]
29+
Output: true
30+
```
31+
32+
Constraints:
33+
34+
- `1 <= nums.length <= 10^4`
35+
- `-2^31 <= nums[i] <= 2^31 - 1`
36+
37+
| ID | Description | Solution | Test | Difficulty |
38+
|:---:|:------------|:----------------------------:|:--------------------------------------------------------------------------------:|:----------:|
39+
| 283 | Move Zeroes | [solution](./Solution283.kt) | [test](../../../../../../src/test/kotlin/exercise/easy/id283/Solution283Test.kt) | Easy |
40+
41+
:top: [Back to all topics](https://github.com/kotler-dev/kotlin-leetcode)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package exercise.easy.id283
2+
3+
class Solution283 {
4+
fun moveZeroes(nums: IntArray): Unit {
5+
if (nums.size >= 2) {
6+
var index = 0
7+
for (num in nums) {
8+
if (num != 0) nums[index++] = num
9+
}
10+
for (i in nums.size - 1 downTo index) {
11+
nums[i] = 0
12+
}
13+
}
14+
}
15+
16+
/*
17+
18+
fun moveZeroes(nums: IntArray): Unit {
19+
if (nums.size >= 2) {
20+
val box = IntArray(nums.size){0}
21+
var index = 0
22+
for (num in nums) {
23+
if (num != 0) box[index++] = num
24+
}
25+
for ((i, num) in box.withIndex()) {
26+
nums[i] = num
27+
}
28+
}
29+
println(nums.toList())
30+
}
31+
32+
fun moveZeroes(nums: IntArray): Unit {
33+
if (nums.size > 2) {
34+
val hm = HashMap<Int, Int>()
35+
var key = 0
36+
37+
for ((index, num) in nums.withIndex()) {
38+
if (num == 0) hm[key] = index
39+
if (num != 0 && hm[key] != null) {
40+
nums[hm[key++]!!] = num
41+
nums[index] = 0
42+
}
43+
}
44+
}
45+
}
46+
47+
*/
48+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package exercise.easy.id283
2+
3+
import io.kotest.assertions.print.print
4+
import io.kotest.core.spec.style.FunSpec
5+
import io.kotest.datatest.WithDataTestName
6+
import io.kotest.datatest.withData
7+
import io.kotest.matchers.equals.shouldBeEqual
8+
import io.kotest.matchers.shouldBe
9+
10+
data class TestCase(
11+
val nums: IntArray,
12+
val expected: IntArray,
13+
) : WithDataTestName {
14+
override fun dataTestName(): String = expected.print().value
15+
}
16+
17+
class Solution283Test : FunSpec({
18+
context("shouldBe") {
19+
withData(
20+
nameFn = { "Input: ${it.nums.print().value}, Expected: ${it.expected.print().value}" },
21+
TestCase(nums = intArrayOf(0, 1, 0, 3, 12), expected = intArrayOf(1, 3, 12, 0, 0)),
22+
TestCase(nums = intArrayOf(0), expected = intArrayOf(0)),
23+
TestCase(nums = intArrayOf(0, 1), expected = intArrayOf(1, 0)),
24+
) { (nums, expected) ->
25+
Solution283().moveZeroes(nums)
26+
nums.toList() shouldBeEqual expected.toList()
27+
}
28+
}
29+
})

0 commit comments

Comments
 (0)