【LeetCode-283】移动数字0至数组尾部,其它数字保持相对位置不变

public static void moveZeroes(int[] nums) {
        
        if (nums == null || nums.length == 0) 
            return;        

        int insertPos = 0;
        // 找非0
        for (int num: nums) {
            if (num != 0) 
                nums[insertPos++] = num;
        }        

        // 补充0
        while (insertPos < nums.length) {
            nums[insertPos++] = 0;
        }
    }

 

posted @ 2020-04-20 14:25  CodeCorner  阅读(150)  评论(0)    收藏  举报