[LeetCode] 27. Remove Element

19 篇文章 0 订阅
11 篇文章 0 订阅

原题链接:https://leetcode.com/problems/remove-element/

1. 题目介绍

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.

给定一个数组 nums 和一个值 val,不能使用额外得数组,在原数组的基础上移除所有数值等于 val 的元素,返回移除后数组的新长度。

不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成。

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

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.5个元素排列的顺序是任意的,不需要考虑返回长度之后的数组

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 是以“引用”方式传递的。也就是说,不对实参作任何拷贝
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]);
}

2. 解题思路-双指针法

这道题和 26. Remove Duplicates from Sorted Array 很像。解法也很类似,都可以使用双指针法解决。

使用2个指针,左指针 left 和右指针 right。left 指向第一个元素,right 指向最后一个元素。我们需要做这样的一个操作 : 当 left 所指向的元素等于 val 时,我们就把 left 和 right 所指元素进行交换。交换之后,right 需要向前走一位,left 不变。重复上述操作,直到 left 所指的元素不等于 val 时,我们才将 left 向后移动一位。

也就是,数组可以分为三个段:
0 ~ left 的元素都是不等于val 的元素,
left ~ right之间是待判断的元素,
right 之后直到结束都是等于 val 的元素。

当 left 等于 right 时,循环结束。

返回数组长度时,不能简单返回指针来代表数组的长度,还需要判断left所指的元素的值究竟是不是val,请看下例:

EX1 :Given nums = [45], val = 4

最终nums = [5 , 4],left 和 right 都为0 ,nums[ left ] != val

EX2 :Given nums = [45], val = 5

最终nums = [4 , 5],left 和 right 都为1 ,nums[ left ] == val

因此可以总结出规律:
当 nums[ left ] != val 时, 正确的长度是 指针+1。
而当 nums[ left ] == val 时,正确的长度是 指针的值。

实现代码

class Solution {
    public int removeElement(int[] nums, int val) {
        int length = nums.length;
        if(length == 0) {
        	return 0;
        }
        
        int left = 0;
        int right = length -1;
        while(  left != right ) {
        	if(nums[left] == val) {
        		nums[left] ^= nums[right];
        		nums[right] ^= nums[left];
        		nums[left] ^= nums[right];
        		right -- ;
        	}else {
        		left++;
        	}
        }
        return (nums[left] == val ? left : left+1);
    }
}
补充:不使用辅助变量实现两数交换

通常情况下,如果想要交换两个数字 a 和 b 的值,必须使用辅助变量temp。

temp = a;
a = b;
b =temp;

能否在不使用 temp 的情况下交换 a 和 b 的值呢?可以使用异或来实现。
异或有这样一个规律:将两数异或的结果,和其中一个数异或,就会得到另一个数。

c = a^b;
b = c^b;
a = c^a;

把中间变量 c 化简掉,可以写成:

a ^= b;
b ^= a;
a ^= b;

上述方法可以实现两数交换,并且不需要辅助变量。

3. 参考资料

26. Remove Duplicates from Sorted Array

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值