My Java Solution


  • 0
    L
    public class Solution {
       public void moveZeroes(int[] nums) {
            if (nums == null || nums.length < 2)
                return;
            int p;
            for (int i = 0; i < nums.length; i++) {
                if (nums[i] == 0) {
                    p = i + 1;
                    if(p >= nums.length)
                        return;
                    while (nums[p] == 0) 
                        if (++p >= nums.length)
                            return;
                    int t = nums[p];
                    nums[p] = nums[i];
                    nums[i] = t;
                }
            }
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.