283. Move Zeroes
Given an array nums
, write a function to move all 0
's to the end of it while maintaining the relative order of the non-zero elements.
Example:
Input: [0,1,0,3,12]
Output: [1,3,12,0,0]
Note :
- You must do this in-place without making a copy of the array.
- Minimize the total number of operations.
# @lc code=start
using LeetCode
# use double pointers
function move_zeros!(nums::Vector{Int})::Nothing
p2 = 1
for p1 in 1:length(nums)
if nums[p1] != 0
nums[p1], nums[p2] = nums[p2], nums[p1]
p2 += 1
end
end
return nothing
end
# @lc code=end
move_zeros! (generic function with 1 method)
This page was generated using DemoCards.jl and Literate.jl.