Array:将0移除

eg:given nums = [0, 1, 0, 3, 12], nums should be [1,3, 12, 0, 0].

public void moveZeroes(int[] nums) {
        int index = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] != 0) {
                nums[index++] = nums[i];
            }
        }
        for (int i = index; i < nums.length; i++) {
            nums[i] = 0;
        }
    }

你可能感兴趣的:(Array:将0移除)