LeetCode 27. 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 by modifying the input array in-place with O(1) extra memory.
The order of elements can be changed. It doesn’t matter what you leave beyond the new length.

Example:

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

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

移除数组中和给定值相同的元素,返回新数组的长度。不允许分配新的空间来制造新的数组,数组元素的顺序可以改变,新数组长度外的元素不需要理会。
这题还是用Two Pointers,一个指向数组当前尾元素(end),一个指向当前元素(i),若当前元素不等于给定值,那么i移到下一个元素;若当前元素等于给定值,就把尾元素的值赋给当前元素,并把end指针向前移一位。

int removeElement(vector<int>& nums, int val) {
        if (nums.size() == 0) return 0;
        int end = nums.size() - 1, i = 0;
        while (i <= end)
        {
            if (nums[i] == val) {
                nums[i] = nums[end];
                --end;
            }
            else ++i;
        }
        return i;
    }
### JavaScript 中数组的 removeElement 实现 在 JavaScript 里,没有原生的 `removeElement` 函数,但可自定义函数来实现从数组中移除元素的功能。比如,`addElement` 函数借助 `push` 方法把新值添加到数组末尾,`removeElement` 函数使用 `splice` 方法移除指定索引处的元素。示例代码如下: ```javascript function addElement(arr, value) { arr.push(value); return arr; } function removeElement(arr, index) { if (index > -1 && index < arr.length) { arr.splice(index, 1); } return arr; } let myArray = [1, 2, 3, 4, 5]; console.log(removeElement(myArray, 2)); // 移除索引为 2 的元素 ``` 上述代码中,`removeElement` 函数接收数组和索引作为参数,通过 `splice` 方法移除指定索引处的元素 [^1]。 ### Java 中 Vector 的 removeElement 方法 在 Java 里,`Vector` 类有 `removeElement` 方法,用于移除指定对象的首个出现项。示例代码如下: ```java import java.util.*; public class RemoveElementOfVector { public static void main(String[] args) { Vector<String> v = new Vector<String>(10); v.add("C"); v.add("C++"); v.add("JAVA"); v.add("C++"); v.add("JAVA"); System.out.println("v: " + v); v.removeElement("JAVA"); System.out.println("v.removeElement(JAVA): " + v); } } ``` 此代码定义了一个 `Vector` 对象,添加元素后,使用 `removeElement` 方法移除首个出现的 `"JAVA"` 元素 [^2]。 ### LeetCode 相关问题的 removeElement 实现 在 LeetCode 问题中,有移除数组中指定元素并返回新数组长度的需求。例如,在已排序数组里移除重复元素,或者移除指定元素。 以下是一个使用双指针方法实现移除指定元素的 Python 代码示例: ```python class Solution: def removeElement(self, A, elem): f = 0 while f != len(A): if A[f] == elem: A.pop(f) else: f += 1 return len(A) ``` 此代码通过遍历数组,若当前元素等于指定元素,则使用 `pop` 方法移除该元素,最终返回新数组的长度 [^4]。 ### 另一种双指针实现思路 另一种常见的实现思路是使用双指针,一个指针遍历数组,另一个指针记录新数组的位置。以下是 Java 代码示例: ```java public class Solution { public int removeElement(int[] A, int elem) { int len = A.length, j = 0; for (int i = 0; i < len; i++) { if (A[i] != elem) { A[j] = A[i]; j++; } } return j; } } ``` 此代码中,`i` 指针遍历数组,`j` 指针记录新数组的位置,若当前元素不等于指定元素,则将其赋值给 `j` 指针指向的位置,最后返回 `j` 作为新数组的长度 [^5]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值