My Simple Java solution without swap


  • 0
    2
    class Solution {
        public void moveZeroes(int[] nums) {
            
            int k = 0;
            for(int i = 0; i < nums.length; ++i){
                if(nums[i] != 0 ){
                    if(k!=i){
                        nums[k] = nums[i];
                        nums[i] = 0;
                    }
                    ++k;
                }
            }
            
        }
    }
    

Log in to reply
 

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