Kotlin Leetcode - 27. Remove Element
題目連接:https://leetcode.com/problems/remove-element/
class Solution {
fun removeElement(nums: IntArray, `val`: Int): Int {
}
}
解題思路
這一題考的是對陣列的處理
Kotlin 參考解答
我們可以用 forEach
處理這題
class Solution {
fun removeElement(nums: IntArray, `val`: Int): Int {
var l = 0
nums.forEach {
if (it != `val`) {
nums[l++] = it
}
}
return l
}
}
回到 leetcode 列表