Leetcode - Move Zeroes

Leetcode - Move Zeroes_第1张图片
Screenshot from 2016-01-25 17:14:49.png

My code:

public class Solution {
    public void moveZeroes(int[] nums) {
        if (nums == null || nums.length == 0)
            return;
        int del = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == 0) {
                del++;
            }
            else {
                nums[i - del] = nums[i];
            }
        }
        for (int i = nums.length - del; i < nums.length; i++)
            nums[i] = 0;
    }
}

没什么难的。但是提交了好几次才过。不可原谅!!
注意,要按照原顺序移动!!

Anyway, Good luck, Richardo!

你可能感兴趣的:(Leetcode - Move Zeroes)