My simple java solution in one loop


  • 1
    A


    public class Solution {
    public void moveZeroes(int[] nums) {
    int end = 0;
    for(int i = 0; i < nums.length; i++) {
    if(nums[i] != 0) {
    if(end != i) { //need to move i
    nums[end] = nums[i];
    nums[i] = 0;
    }
    end++;
    }
    }
    }

    }


Log in to reply
 

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