Swift solution - Two Pointers


  • 0
    class Solution {
        func moveZeroes(_ nums: inout [Int]) {
            var i = 0
            var j = 0
            
            while i < nums.count {
                if nums[i] != 0 {
                    if nums[i] != nums[j] {
                        swap(&nums[i], &nums[j])
                    }
                    j += 1
                }
                i += 1
            }
        }
    }
    

Log in to reply
 

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