O(1) Space Java Solution time O(n)


  • 0
    D
    	public void moveZeroes(int[] nums) {
    	int c = 0;
    	for (int i = 0; i < nums.length - 1; i++) {
    		if (nums[i] == 0 && nums[i + 1] != 0) {
    			nums[i + 1] = nums[i - c] ^ nums[i + 1];
    			nums[i - c] = nums[i + 1] ^ nums[i - c];
    			nums[i + 1] = nums[i + 1] ^ nums[i - c];
    			c = 0;
    			while (nums[i--] == 0);
    		} else {
    			if (nums[i] == 0 && nums[i + 1] == 0) {
    				c++;
    			}
    		}
    	}
    }

Log in to reply
 

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