From 5c777cc6fb802c053dfe83b238920d7f816f0a9c Mon Sep 17 00:00:00 2001 From: Kagura Date: Tue, 3 Sep 2024 20:52:57 +0800 Subject: [PATCH] 09start --- src/Test27.kt | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/Test27.kt diff --git a/src/Test27.kt b/src/Test27.kt new file mode 100644 index 0000000..0de8f45 --- /dev/null +++ b/src/Test27.kt @@ -0,0 +1,47 @@ +class Test27 { + class Solution { + fun removeElement(nums: IntArray, `val`: Int): Int { + if (nums.isEmpty()){ + return 0 + } + if (nums.size == 1){ + return if (nums[0] == `val`) 0 else 1 + } + + var l = 0 + var r = nums.size - 1 + while (r >= 0 && nums[r] == `val`){ + r-- + } + + while (l <= r){ + if (nums[l] == `val`){ + nums[l] = nums[r] + do { + r-- + } while (nums[r] == `val`) + } + l++ + } + return l + } + } + + fun test(): Unit { + val s = Solution() + val a1 = arrayOf(3,2,2,3).toIntArray() + println(s.removeElement(a1,3)) + a1.forEach { print(it) } + println() + + val a2 = intArrayOf(0,1,2,2,3,0,4,2) + println(s.removeElement(a2,2)) + a2.forEach { print(it) } + println() + + val a3 = intArrayOf(3,3) + println(s.removeElement(a3,3)) + a3.forEach { print(it) } + println() + } +} \ No newline at end of file