LeetCode 算法题 – Remove Element

本页内容

移除元素:

  • 移除整数数组中值与给定数字相等的元素,并返回其最终的长度
  • 不能分配额外的内存

原题

Given an array and a value, remove all instances of that value in place and return the new length.

Do not allocate extra space for another array, you must do this in place with constant memory.

The order of elements can be changed. It doesn’t matter what you leave beyond the new length.

Example:

Given input array nums = [3,2,2,3], val = 3

Your function should return length = 2, with the first two elements of nums being 2.

分析

本题难度低,主要的为题是不能分配额外的内存,所以需要在原数组进行修改,那么接下来就很简单了,
遍历数组,将和给定数值相等的元素移除即可。

实现

 1func removeElement(nums []int, val int) int {
 2    for i := 0; i < len(nums); {
 3        if nums[i] == val {
 4            // remove current element from nums,
 5            // and the next element's index is i, not i++.
 6            nums = append(nums[:i], nums[i+1:]...)
 7        } else {
 8            i++
 9        }
10    }
11
12    return len(nums)
13}
razonyang
2024年7月23日星期二 2018年9月2日星期日