力扣00027.移除元素


题目描述

给你一个数组 nums 和一个值 val,你需要 原地 移除所有数值等于 val 的元素,并返回移除后数组的新长度。

不要使用额外的数组空间,你必须仅使用 O(1) 额外空间并 原地 修改输入数组。

元素的顺序可以改变。你不需要考虑数组中超出新长度后面的元素。

说明:

为什么返回数值是整数,但输出的答案是数组呢?

请注意,输入数组是以「引用」方式传递的,这意味着在函数里修改输入数组对于调用者是可见的。

你可以想象内部操作如下:

1
2
3
4
5
6
7
8
// nums 是以“引用”方式传递的。也就是说,不对实参作任何拷贝
int len = removeElement(nums, val);

// 在函数里修改输入数组对于调用者是可见的。
// 根据你的函数返回的长度, 它会打印出数组中 该长度范围内 的所有元素。
for (int i = 0; i < len; i++) {
print(nums[i]);
}

示例 1:

输入:nums = [3,2,2,3], val = 3
输出:2, nums = [2,2]
解释:函数应该返回新的长度 2, 并且 nums 中的前两个元素均为 2。你不需要考虑数组中超出新长度后面的元素。例如,函数返回的新长度为 2 ,而 nums = [2,2,3,3] 或 nums = [2,2,0,0],也会被视作正确答案。

示例 2:

输入:nums = [0,1,2,2,3,0,4,2], val = 2
输出:5, nums = [0,1,3,0,4]
解释:函数应该返回新的长度 5, 并且 nums 中的前五个元素为 0, 1, 3, 0, 4。注意这五个元素可为任意顺序。你不需要考虑数组中超出新长度后面的元素。

提示:

  • 0 <= nums.length <= 100
  • 0 <= nums[i] <= 50
  • 0 <= val <= 100

解决方法

C++

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public:
int removeElement(std::vector<int>& nums, int val) {
int i = 0;
for (int j = 0; j < nums.size(); ++j) {
if (nums[j] != val) {
nums[i] = nums[j];
++i;
}
}
return i;
}
};

结果

执行用时 : 3 ms, 击败 44.36% 使用 C++ 的用户

内存消耗 : 10.24 MB, 击败 5.12% 使用 C++ 的用户


Java

1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public int removeElement(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;
}
}

结果

执行用时 : 0 ms, 击败 100.00% 使用 Java 的用户

内存消耗 : 41.04 MB, 击败 5.14% 使用 Java 的用户


Python

1
2
3
4
5
6
7
8
class Solution(object):
def removeElement(self, nums, val):
i = 0
for j in range(len(nums)):
if nums[j] != val:
nums[i] = nums[j]
i += 1
return i

结果

执行用时 : 11 ms, 击败 94.27% 使用 Python 的用户

内存消耗 : 11.30 MB, 击败 99.31% 使用 Python 的用户


Python3

1
2
3
4
5
6
7
8
class Solution:
def removeElement(self, nums: List[int], val: int) -> int:
i = 0
for j in range(len(nums)):
if nums[j] != val:
nums[i] = nums[j]
i += 1
return i

结果

执行用时 : 23 ms, 击败 99.89% 使用 Python3 的用户

内存消耗 : 16.32 MB, 击败 34.59% 使用 Python3 的用户


C

1
2
3
4
5
6
7
8
9
10
int removeElement(int* nums, int numsSize, int val) {
int i = 0;
for (int j = 0; j < numsSize; ++j) {
if (nums[j] != val) {
nums[i] = nums[j];
++i;
}
}
return i;
}

结果

执行用时 : 6 ms, 击败 5.58% 使用 C 的用户

内存消耗 : 5.76 MB, 击败 95.20% 使用 C 的用户


C#

1
2
3
4
5
6
7
8
9
10
11
12
public class Solution {
public int RemoveElement(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;
}
}

结果

执行用时 : 107 ms, 击败 75.62% 使用 C# 的用户

内存消耗 : 45.52 MB, 击败 5.23% 使用 C# 的用户


JavaScript

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
* @param {number[]} nums
* @param {number} val
* @return {number}
*/
var removeElement = function(nums, val) {
let i = 0;
for (let j = 0; j < nums.length; ++j) {
if (nums[j] !== val) {
nums[i] = nums[j];
++i;
}
}
return i;
};

结果

执行用时 : 58 ms, 击败 70.84% 使用 JavaScript 的用户

内存消耗 : 49.11 MB, 击败 6.76% 使用 JavaScript 的用户


TypeScript

1
2
3
4
5
6
7
8
9
10
function removeElement(nums: number[], val: number): number {
let i = 0;
for (let j = 0; j < nums.length; ++j) {
if (nums[j] !== val) {
nums[i] = nums[j];
++i;
}
}
return i;
}

结果

执行用时 : 70 ms, 击败 24.71% 使用 TypeScript 的用户

内存消耗 : 51.71 MB, 击败 5.21% 使用 TypeScript 的用户


PHP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {

/**
* @param Integer[] $nums
* @param Integer $val
* @return Integer
*/
function removeElement(&$nums, $val) {
$i = 0;
foreach ($nums as $j => $num) {
if ($num !== $val) {
$nums[$i] = $num;
++$i;
}
}
return $i;
}
}

结果

执行用时 : 7 ms, 击败 70.04% 使用 PHP 的用户

内存消耗 : 20.02 MB, 击败 5.29% 使用 PHP 的用户


Swift

1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
func removeElement(_ nums: inout [Int], _ val: Int) -> Int {
var i = 0
for j in 0..<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
class Solution {
fun removeElement(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
class Solution {
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
func removeElement(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}
def remove_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
object Solution {
def removeElement(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
impl Solution {
pub fn remove_element(nums: &mut Vec<i32>, val: i32) -> i32 {
let mut i = 0;
for j in 0..nums.len() {
if nums[j] != val {
nums[i] = nums[j];
i += 1;
}
}
i as i32
}
}

结果

执行用时 : 0 ms, 击败 100.00% 使用 Rust 的用户

内存消耗 : 2.03 MB, 击败 62.39% 使用 Rust 的用户


Racket

暂时未解决

1

结果

执行用时 : ms, 击败 % 使用 Racket 的用户

内存消耗 : MB, 击败 % 使用 Racket 的用户