Created
April 5, 2020 09:13
-
-
Save javi-aire/18129e3330596df2643fcba819fa12be to your computer and use it in GitHub Desktop.
Problem 4/30 of LeetCode 30-day challenge
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {number[]} nums | |
* @return {void} Do not return anything, modify nums in-place instead. | |
*/ | |
let moveZeroes = function(nums) { | |
// two pointers, left & right for beginning and end of array | |
let left = 0; | |
let right = nums.length-1; | |
while(left < right) { | |
let element = nums[left]; | |
if(element === 0) { | |
nums.splice(left, 1) | |
nums.splice(nums.length, 0, element); | |
// decrease right pointer after moving 0 to end | |
right--; | |
} else { | |
// increase left pointer if elements !== 0 | |
left++; | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment