From f820e34236c77c2c4ec2b10077c3cd4f3dbb735e Mon Sep 17 00:00:00 2001 From: Kotler Date: Sun, 22 Oct 2023 22:29:07 +0300 Subject: [PATCH] exercise/id283 --- README.md | 7 +-- .../exercise/easy/id283/Description283.md | 46 ++++++++++++++++++ .../kotlin/exercise/easy/id283/Solution283.kt | 48 +++++++++++++++++++ .../exercise/easy/id283/Solution283Test.kt | 29 +++++++++++ 4 files changed, 127 insertions(+), 3 deletions(-) create mode 100644 src/main/kotlin/exercise/easy/id283/Description283.md create mode 100644 src/main/kotlin/exercise/easy/id283/Solution283.kt create mode 100644 src/test/kotlin/exercise/easy/id283/Solution283Test.kt diff --git a/README.md b/README.md index 62d2fa9..6094ede 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/src/main/kotlin/exercise/easy/id283/Description283.md b/src/main/kotlin/exercise/easy/id283/Description283.md new file mode 100644 index 0000000..07016c6 --- /dev/null +++ b/src/main/kotlin/exercise/easy/id283/Description283.md @@ -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) diff --git a/src/main/kotlin/exercise/easy/id283/Solution283.kt b/src/main/kotlin/exercise/easy/id283/Solution283.kt new file mode 100644 index 0000000..f1af637 --- /dev/null +++ b/src/main/kotlin/exercise/easy/id283/Solution283.kt @@ -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() + 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 + } + } + } + } + + */ +} \ No newline at end of file diff --git a/src/test/kotlin/exercise/easy/id283/Solution283Test.kt b/src/test/kotlin/exercise/easy/id283/Solution283Test.kt new file mode 100644 index 0000000..7313354 --- /dev/null +++ b/src/test/kotlin/exercise/easy/id283/Solution283Test.kt @@ -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() + } + } +}) \ No newline at end of file