This commit is contained in:
Kagura 2024-09-03 20:52:57 +08:00
parent dea7cf2f92
commit 5c777cc6fb

47
src/Test27.kt Normal file
View file

@ -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()
}
}