Java Two Pointers


  • 0
    S

    public void moveZeroes(int[] nums) {

    if(nums==null||nums.length==0) return;
    int zero_pointer=0, non_zero_pointer=0;
    
    while(zero_pointer<nums.length&&non_zero_pointer<nums.length){
    	if(nums[zero_pointer]!=0){
    		zero_pointer++;
    		non_zero_pointer++;
    		continue;
    	}
    	if(nums[non_zero_pointer]==0){
    		non_zero_pointer++;
    		continue;
    	}
    	
    	nums[zero_pointer]=nums[non_zero_pointer];
    	nums[non_zero_pointer]=0;
    }
    

    }


Log in to reply
 

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