O(N) Javascript


  • 0
    R

    What do you guys think of this solution, it was accepted , can i optimize it further

    /**

    • @param {number[]} nums
    • @return {void} Do not return anything, modify nums in-place instead.
      */
      var moveZeroes = function(nums) {
      var noOfZ =0;
      var posToFill =0;
      var lenOfNums = nums.length;
      for(var i =0;i<lenOfNums;i++){
      if(nums[i]===0){
      noOfZ++;
      }
      if(nums[i]!==0){
      nums[posToFill] = nums[i];
      posToFill++;
      }
      }
      lenOfNums = lenOfNums-1;
      while(noOfZ>0){
      nums[lenOfNums] = 0;
      lenOfNums--;
      noOfZ--;
      }

    };


Log in to reply
 

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