每天一个小程序(十)--- Remove Element

21 篇文章 0 订阅
14 篇文章 0 订阅

RemoveElement

俺の命は君のものだ、アスナ、だから君のために使う、最后の一瞬まで一绪にいよう。

刀剑神域

前言:

晕哦,最近好忙,还封网了,今天一看连leetcode都上不去了,只有拿点存货出来


package array;

/**
 * @author BlackSugar
 * @date 2019/4/16
 * Given an array nums and a value val, remove all instances of that value in-place and return the new length.
 * <p>
 * Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
 * <p>
 * The order of elements can be changed. It doesn't matter what you leave beyond the new length.
 * <p>
 * Example 1:
 * <p>
 * Given nums = [3,2,2,3], val = 3,
 * <p>
 * Your function should return length = 2, with the first two elements of nums being 2.
 * <p>
 * It doesn't matter what you leave beyond the returned length.
 * Example 2:
 * <p>
 * Given nums = [0,1,2,2,3,0,4,2], val = 2,
 * <p>
 * Your function should return length = 5, with the first five elements of nums containing 0, 1, 3, 0, and 4.
 * <p>
 * Note that the order of those five elements can be arbitrary.
 * <p>
 * It doesn't matter what values are set beyond the returned length.
 * Clarification:
 * <p>
 * Confused why the returned value is an integer but your answer is an array?
 * <p>
 * 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.
 * <p>
 * Internally you can think of this:
 * <p>
 * // nums is passed in by reference. (i.e., without making a copy)
 * int len = removeElement(nums, val);
 * <p>
 * // 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]);
 * }
 */
public class RemoveElement {
    /**
     * int数组隐藏目标参数
     * 思路:由于要求O(1)空间复杂度,因此没办法真正改变int数组,而是跳过目标数字,在前n个放入非目标数字,不用管后面的数字
     * 遍历数组,若和目标参数相等则跳过,返回新数组不相等数字的长度
     *
     * @param nums
     * @param val
     * @return
     */
    public int removeElement(int[] nums, int val) {
        int preIndex;
        int currentIndex = 0;
        for (preIndex = 0; preIndex < nums.length; preIndex++) {
            if (nums[preIndex] != val) {
                nums[currentIndex] = nums[preIndex];
                currentIndex++;
            }
        }
        return currentIndex;
    }

    public static void main(String[] args) {
        int[] array = new int[]{1, 2, 2, 3};
        int len = new RemoveElement().removeElement(array, 2);
        System.out.println("length: " + len);
        for (int i = 0; i < len; i++) {
            System.out.print(array[i]);
        }
        System.out.println();
    }
}

总结:

这道题很多人点了 ?,因为要求只能用O(1)的空间复杂度,这个完全打乱了原始数组,某种意义上算是cheat,采用双指针,一个遍历,一个记录当前位置即可

1、时间复杂度O(n)
2、空间复杂度O(1)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值