[LeetCode] 27. 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.

题意:从一个数组中删除与val相应的元素,并返回删除后的长度
先理解这里的删除,其实就是都放到队尾
解法一:双指针,头换尾,尾的值是val就是往前移动,
class Solution {
    public int removeElement(int[] nums, int val) {
        int sum = 0;
        int i = 0;
        int j = nums.length - 1;
        while (i <= j) {
            if (i == j) {
                if (nums[i] == val)
                    sum ++;
                break;
            }
            if (nums[i] == val) {
                while (nums[j] == val) {
                    j--;sum ++;
                    if (j < 0)
                        break;
                    if (j == i)
                        break;
                }
                if (j < 0) {
                    break;
                }
                swap(nums, i, j);
                j --;sum++;
            } else {
                i ++;
            }
        }
        return nums.length - sum;
    }
    private void swap(int[] arr, int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
}

解法二:边移动边换
class Solution {
    public int removeElement(int[] nums, int val) {
        int j = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == val) {
                nums[j] = nums[i];
                j ++;
            }
        }
        return j;
    }
}

 

解法三:先排序,然后操作,因为排了序之后所有相同的val就都在一起了

(其实这里我不是很能理解,为什么这个O(nlogn)的算法确快了,这个代码我是抄的,因为一直想着应该是O(n)的算法一开始就没想过排序操作 (笑)

后来我又审查了我的代码,可能在n比较小的时候,可能由于交换需要的代价比较大吧,)

class Solution {
    public int removeElement(int[] nums, int val) {
        
        Arrays.sort(nums);
        
        int result=0;//未与k重复元素的下标
            
        for(int i=0;i<nums.length;i++){

            if(nums[i]==val){
                
                //在数组中i之后找到第一个与k不相等的元素的下标
                i=noEqualsK(nums,i+1,val);
                for(;i<nums.length;i++){
                    
                    nums[result++]=nums[i];
                    
                }
                
            }else{
                
                result++;
                
            }
        }
        
        return result;

    }
    //返回数组中i之后找到第一个与k不相等的元素的下标
    public int noEqualsK(int[] nums,int i,int val){
        
        for(;i<nums.length;i++){

            if(nums[i]!=val){
                
                return i;
                
            }
        }
        
        return nums.length;
    }
    
}

 



转载于:https://www.cnblogs.com/Moriarty-cx/p/9751025.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值