Files
leetcode/array/1binary search/27remove_target/kotlin/App.kt
flowerstonezl e1006fa09c first init
2026-01-29 17:04:05 +08:00

50 lines
1.1 KiB
Kotlin

/*
* This Kotlin source file was generated by the Gradle 'init' task.
*/
package binarySearch
class Solution {
fun removeElement(nums: IntArray, target: Int): Int {
var fast_index :Int = 0
var slow_index :Int = 0
while (fast_index < nums.size) {
if (nums[fast_index] != target) {
nums[slow_index] = nums[fast_index]
slow_index++
}
fast_index++
}
return slow_index
}
}
// 测试用例
fun main() {
val nums = intArrayOf(0, 1, 2, 3, 4, 5, 6, 7, 8)
println("请输入要剔除的数字:")
val input = readLine()
// 验证输入有效性
if (input.isNullOrBlank()) {
println("错误:未输入任何内容")
return
}
val target = input.toIntOrNull() ?: run {
println("错误:请输入有效的整数")
return
}
val solution = Solution()
val result = solution.removeElement(nums, target )
if (result == -1){
println("不存在$target")
}
else{
println("删除后数组长度: $result")
}
}