【快慢指针】283.移动零

题目

法1:快慢指针

必须掌握,基础算法!!!

class Solution {
    public void moveZeroes(int[] nums) {
        int slow = -1, fast = 0;
        while (fast < nums.length) {
            if (nums[fast] != 0) {
                nums[++slow] = nums[fast++];
            } else {
                fast++;
            }
        }

        while (slow + 1 < nums.length) {
            nums[++slow] = 0;
        }
    }
}

你可能感兴趣的:(快慢指针)