Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,13 @@ My Kotlin exercises with tests from [Leetcode](https://leetcode.com/kotlerdev)

#### Array

| ID | Description | Solution | Test | Difficulty |
|:---:|:-------------------------------------------------------------------------------------------------|:-----------------------------------------------------------------:|:-----------------------------------------------------------------:|:----------:|
| ID | Description | Solution | Test | Difficulty |
|:---:|:----------------------------------------------------------------------------------------------|:-----------------------------------------------------------------:|:-----------------------------------------------------------------:|:----------:|
| 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 |
| 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 |
| 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 |
| 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 |
| 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 |
| 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 |

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

Expand Down
46 changes: 46 additions & 0 deletions src/main/kotlin/exercise/easy/id283/Description283.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
[//]: # (Copyright [2023] [Anton Kotler kotler.developer@gmail.com] License MIT)

# Move Zeroes

Given an integer array `nums`, move all `0`'s to the end of it while maintaining the relative order of the non-zero
elements.
Note that you must do this in-place without making a copy of the array.

![Difficulty](https://img.shields.io/badge/Difficulty-Easy-61904f)

Example 1:

[0, 1, 0, 3, 12]

[index 1]
[1, 0]

```
Input: nums = [0,1,0,3,12]
Output: [1,3,12,0,0]
```

Example 2:

```
Input: nums = [0]
Output: [0]
```

Example 3:

```
Input: nums = [1,1,1,3,3,4,3,2,4,2]
Output: true
```

Constraints:

- `1 <= nums.length <= 10^4`
- `-2^31 <= nums[i] <= 2^31 - 1`

| ID | Description | Solution | Test | Difficulty |
|:---:|:------------|:----------------------------:|:--------------------------------------------------------------------------------:|:----------:|
| 283 | Move Zeroes | [solution](./Solution283.kt) | [test](../../../../../../src/test/kotlin/exercise/easy/id283/Solution283Test.kt) | Easy |

:top: [Back to all topics](https://github.com/kotler-dev/kotlin-leetcode)
48 changes: 48 additions & 0 deletions src/main/kotlin/exercise/easy/id283/Solution283.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package exercise.easy.id283

class Solution283 {
fun moveZeroes(nums: IntArray): Unit {
if (nums.size >= 2) {
var index = 0
for (num in nums) {
if (num != 0) nums[index++] = num
}
for (i in nums.size - 1 downTo index) {
nums[i] = 0
}
}
}

/*

fun moveZeroes(nums: IntArray): Unit {
if (nums.size >= 2) {
val box = IntArray(nums.size){0}
var index = 0
for (num in nums) {
if (num != 0) box[index++] = num
}
for ((i, num) in box.withIndex()) {
nums[i] = num
}
}
println(nums.toList())
}

fun moveZeroes(nums: IntArray): Unit {
if (nums.size > 2) {
val hm = HashMap<Int, Int>()
var key = 0

for ((index, num) in nums.withIndex()) {
if (num == 0) hm[key] = index
if (num != 0 && hm[key] != null) {
nums[hm[key++]!!] = num
nums[index] = 0
}
}
}
}

*/
}
29 changes: 29 additions & 0 deletions src/test/kotlin/exercise/easy/id283/Solution283Test.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package exercise.easy.id283

import io.kotest.assertions.print.print
import io.kotest.core.spec.style.FunSpec
import io.kotest.datatest.WithDataTestName
import io.kotest.datatest.withData
import io.kotest.matchers.equals.shouldBeEqual
import io.kotest.matchers.shouldBe

data class TestCase(
val nums: IntArray,
val expected: IntArray,
) : WithDataTestName {
override fun dataTestName(): String = expected.print().value
}

class Solution283Test : FunSpec({
context("shouldBe") {
withData(
nameFn = { "Input: ${it.nums.print().value}, Expected: ${it.expected.print().value}" },
TestCase(nums = intArrayOf(0, 1, 0, 3, 12), expected = intArrayOf(1, 3, 12, 0, 0)),
TestCase(nums = intArrayOf(0), expected = intArrayOf(0)),
TestCase(nums = intArrayOf(0, 1), expected = intArrayOf(1, 0)),
) { (nums, expected) ->
Solution283().moveZeroes(nums)
nums.toList() shouldBeEqual expected.toList()
}
}
})