Why it doesn't work??


  • 0
    N
    public class Solution {
        public void moveZeroes(int[] nums) {
            int count = 0;
            for(int i=0; i<nums.length; i++){
                if(nums[i]==0){
                    count++;
                    for(int j=i; j<nums.length-1; j++){
                        nums[j]=nums[j+1];
                    }
                }
            }
            for(int i=0; i<count; i++){
                nums[nums.length-count+i] = 0;
            }
        }
    } 
    

    It got the right answer [1,3,12,0,0] when the input is [0,1,0,3,12].
    but It got [0,0,0] when the input is [0,1,0].


Log in to reply
 

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