My O(n) solution,very easy


  • 0
    U

    public class Solution {
    public void moveZeroes(int[] nums) {

    	int forwardStep = 0;
    	
    	for(int i = 0 ; i < nums.length; i++){
    		if(nums[i] == 0){
    			forwardStep ++;
    			continue;
    		}
    		nums[i - forwardStep] = nums[i];
    	}
    	
    	for(int i = nums.length - 1 ; i > nums.length - 1-forwardStep; i--){
    		nums[i] = 0;
    	}
    	
    }
    

    }


Log in to reply
 

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