My super easy to understand Code in Java within 1ms running time


  • 0
    S
    public class Solution {
        public void moveZeroes(int[] nums) {
            int i=0;
            int j=0;
            for(i=0;i<nums.length;i++){
                if(nums[i]==0){
                    //hold j which point to the first 0
                }
                else{
                    int temp;
                    temp=nums[i];   //swith nums[i] and nums[j]
                    nums[i]=nums[j];
                    nums[j]=temp;
                    j++;  //j move to next pos, which is still the first pos of 0
                }
            }
        }
    }

Log in to reply
 

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