Created
December 7, 2019 05:14
-
-
Save eebasadre20/88be3fcfe13ee664473342bb3b081212 to your computer and use it in GitHub Desktop.
LeetCode - Move Zeroes
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 {Integer[]} nums | |
| # @return {Void} Do not return anything, modify nums in-place instead. | |
| def move_zeroes(nums) | |
| x = 0 | |
| y = x + 1 | |
| loop do | |
| break if y >= nums.length | |
| if nums[x] == 0 && nums[y] != 0 | |
| nums[x], nums[y] = nums[y], nums[x] | |
| x += 1 | |
| y += 1 | |
| else | |
| x += 1 if nums[x] != 0 | |
| y += 1 | |
| end | |
| end | |
| nums | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment