a simple Java solution. like partition in quick sort.


  • 0

    It's something like partition in quick sort.

    public class Solution {
        public void moveZeroes(int[] nums) {
            int idx = -1;
            for(int i = 0; i < nums.length; i++) {
                if(nums[i] != 0 && ++idx != i) {
                    nums[idx] = nums[i];
                }
            }
            for(int i = idx + 1; i < nums.length; i++) {
                nums[i] = 0;
            }
        }
    }
    

Log in to reply
 

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