classSolution { funcremoveElement(_nums: inout [Int], _val: Int) -> Int { var i =0 for j in0..<nums.count { if nums[j] != val { nums[i] = nums[j] i +=1 } } return i } }
结果
执行用时 : 6 ms, 击败 48.38% 使用 Swift 的用户
内存消耗 : 15.58 MB, 击败 5.19% 使用 Swift 的用户
Kotlin
1 2 3 4 5 6 7 8 9 10 11 12
classSolution { funremoveElement(nums: IntArray, `val`: Int): Int { var i = 0 for (j in nums.indices) { if (nums[j] != `val`) { nums[i] = nums[j] i++ } } return i } }
结果
执行用时 : 164 ms, 击败 79.06% 使用 Kotlin 的用户
内存消耗 : 34.39 MB, 击败 11.96% 使用 Kotlin 的用户
Dart
1 2 3 4 5 6 7 8 9 10 11 12
classSolution{ int removeElement(List<int> nums, int val) { int i = 0; for (int j = 0; j < nums.length; j++) { if (nums[j] != val) { nums[i] = nums[j]; i++; } } return i; } }
结果
执行用时 : 318 ms, 击败 25.00% 使用 Dart 的用户
内存消耗 : 146.96 MB, 击败 50.00% 使用 Dart 的用户
Go
1 2 3 4 5 6 7 8 9 10
funcremoveElement(nums []int, val int)int { i := 0 for j := 0; j < len(nums); j++ { if nums[j] != val { nums[i] = nums[j] i++ } } return i }
结果
执行用时 : 0 ms, 击败 100.00% 使用 Go 的用户
内存消耗 : 2.01 MB, 击败 68.18% 使用 Go 的用户
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13
# @param {Integer[]} nums # @param {Integer} val # @return {Integer} defremove_element(nums, val) i = 0 nums.each_with_index do |num, j| if num != val nums[i] = num i += 1 end end i end
结果
执行用时 : 62 ms, 击败 62.50% 使用 Ruby 的用户
内存消耗 : 206.46 MB, 击败 25.00% 使用 Ruby 的用户
Scala
1 2 3 4 5 6 7 8 9 10 11 12
objectSolution{ defremoveElement(nums: Array[Int], `val`: Int): Int = { var i = 0 for (j <- nums.indices) { if (nums(j) != `val`) { nums(i) = nums(j) i += 1 } } i } }
结果
执行用时 : 483 ms, 击败 30.77% 使用 Scala 的用户
内存消耗 : 54.35 MB, 击败 30.77% 使用 Scala 的用户
Rust
1 2 3 4 5 6 7 8 9 10 11 12
implSolution { pubfnremove_element(nums: &mutVec<i32>, val: i32) ->i32 { letmut i = 0; forjin0..nums.len() { if nums[j] != val { nums[i] = nums[j]; i += 1; } } i asi32 } }