Problem List

Move Zeroes

May 31, 2025Go array, two pointers, in-placeeasy

Problem

Approach

Edge Cases

Reflections

Time performance came in around the 19th percentile, which is really back. Too many nested scans. Memory was solid, 97th percentile not that it matters much. It did the job but had no hustle. In future solutions, I’ll switch to the cleaner two-pointer method that moves non-zeros forward directly and leaves zeros behind.

Performance

Complexity

Go Solution
func moveZeroes(nums []int)  {
    for i := range(nums) {
        if nums[i] == 0 {
            j := i
            for nums[j] == 0 && j < len(nums) - 1 {
                j++
            }
            nums[i], nums[j] = nums[j], nums[i]
        }
    }
}
LeetCode Problem Link