[算法][力扣283]Move Zeros

题号:283

给定一个数组nums,写一个函数,将数组中所有的0挪到数组的末尾,而维持其他所有非0元素的相对位置。

解法一:把非零的数放到数组前,然后后面的位置设置为0

java 复制代码
/**
 * 给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。
 * 请注意 ,必须在不复制数组的情况下原地对数组进行操作。
 */
public class Test01_283_001 {

    public static void main(String[] args) {
        moveZeroes(new int[]{0, 0, 1, 0, 0, 0, 3, 12});
    }

    public static void moveZeroes(int[] nums) {
        // 将非零数据往前移动
        int index = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == 0) {
                continue;
            }
            nums[index] = nums[i];
            index++;
        }

        // 后面的数据都设置为0
        for (int i = index; i < nums.length; i++) {
            nums[i] = 0;
        }
        System.out.println(Arrays.toString(nums));
    }
}

解法二:使用两个指针

java 复制代码
/**
 * 给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。
 * 请注意 ,必须在不复制数组的情况下原地对数组进行操作。
 */
public class Test01_283_2 {

    public static void main(String[] args) {
        moveZeroes(new int[]{1});
    }

    public static void moveZeroes(int[] nums) {
        // 使用left、right两个指针,left不为0,right为left+1
        // 首先找到left为0的位置
        int left = 0;
        while (left < nums.length) {
            if (nums[left] == 0) {
                break;
            }
            left++;
        }

        // left和right交换
        for (int right = left + 1; right < nums.length; right++) {
            if (nums[right] != 0 && left != right) {
                int temp = nums[right];
                nums[right] = nums[left];
                nums[left] = temp;
                left++;
            }
        }
        System.out.println(Arrays.toString(nums));
    }
}

双指针的另一种写法:

java 复制代码
/**
 * 给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。
 * 请注意 ,必须在不复制数组的情况下原地对数组进行操作。
 */
public class Test01_283_002 {

    public static void main(String[] args) {
        moveZeroes(new int[]{5, 0, 1, 0, 0, 0, 3, 12});
    }

    public static void moveZeroes(int[] nums) {
        int left = 0;
        int right = 0;
        while (right < nums.length) {
            if (nums[right] == 0) {
                right++;
            } else if (nums[right] != 0 && left < right) {
                ArrayUtils.swap(nums, left, right);
                left++;
                right++;
            } else { // nums[right] != 0 && left == right
                left++;
                right++;
            }
        }
        System.out.println(Arrays.toString(nums));
    }
}
相关推荐
To_OC4 小时前
LC 994 腐烂的橘子:人人都说是 BFS 入门题,我却写了三遍才过
javascript·算法·leetcode
金銀銅鐵7 小时前
[Python] 扩展欧几里得算法
python·数学·算法
To_OC10 小时前
LC 200 岛屿数量:经典 DFS 入门题,我第一次写居然连方向都搞错了
javascript·算法·leetcode
To_OC1 天前
LC 128 最长连续序列:别上来就排序,O (n) 解法才是这题的灵魂
javascript·算法·leetcode
05Kevin2 天前
lk每日冒险题--数据结构6.27
算法
To_OC2 天前
从一次栈溢出报错说起,我把递归彻底扒明白了
javascript·算法·程序员
千纸鹤安安2 天前
千问Qwen-AgentWorld来了:一个语言模型搞定七大Agent场景,GPT-5.4都输了
算法
七牛开发者2 天前
MCP 到底是什么?为什么 Agent 都想接上它
算法·aigc·agent