Remove Element

本文介绍LeetCode上一道经典题目“移除元素”的解题思路与Java实现。题目要求在原地移除数组中指定值的所有实例,并返回新长度。文章提供了详细的算法解析及代码示例。

https://leetcode.com/problems/remove-element/

题目

Given an array nums and a value val, 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 1:

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

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

It doesn't matter what you leave beyond the returned length.
Example 2:

Given nums = [0,1,2,2,3,0,4,2], val = 2,

Your function should return length = 5, with the first five elements of nums containing 0, 1, 3, 0, and 4.

Note that the order of those five elements can be arbitrary.

It doesn't matter what values are set beyond the returned length.
Clarification:

Confused why the returned value is an integer but your answer is an array?

Note that the input array is passed in by reference, which means modification to the input array will be known to the caller as well.

Internally you can think of this:

// nums is passed in by reference. (i.e., without making a copy)
int len = removeElement(nums, val);

// any modification to nums in your function would be known by the caller.
// using the length returned by your function, it prints the first len elements.
for (int i = 0; i < len; i++) {
    print(nums[i]);
}

题意:在nums数组中去掉与val相等的值。
AC code:

import java.util.ArrayList;
import java.util.List;

class Solution {
    public int removeElement(int[] nums, int val) {
        List<Integer> list = new ArrayList<Integer>();
        for (int i = 0; i < nums.length; i++) {
			if(nums[i]!=val) {
				list.add(nums[i]);
			}
		}
        Object[] objs=list.toArray();
        for(int i=0;i<objs.length;i++) {
        	nums[i]=(int)objs[i];
        }
        return objs.length;
    }
}
### 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、付费专栏及课程。

余额充值