My Java Solution


  • 0
    C
    public class Solution {
        public void moveZeroes(int[] nums) {
            if (nums == null || nums.length == 0)
                return ;
            int toZeroIndex = 0; 
            int toNonZeroIndex = 0;
            for (int i = 0 ;i < nums.length; i++)
            {
                if (nums[i] == 0)
                {
                    toNonZeroIndex++;
                }
                else
                {
                    int temp = nums[toZeroIndex];
                    nums[toZeroIndex] = nums[toNonZeroIndex];
                    nums[toNonZeroIndex] = temp;
                    // swap the value of zero and non-zero
                    toZeroIndex++;
                    toNonZeroIndex++;
                }
            }
        }
    }

Log in to reply
 

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