O(n) Solution Java 1.1ms


  • 5
    A
    public class Solution {
    public void moveZeroes(int[] nums) {
       int index = -1;
    	for(int i = 0; i < nums.length; i++){
    		//记录0第一次出现的位置
    		if(index == -1&&nums[i]==0){
    			index = i;
    		}
    		if(nums[i]!=0&&index!=-1){
    			nums[index] = nums[i];
    			nums[i] = 0;
    			index++;
    		}
    	}
    }
    

    }


Log in to reply
 

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