A Swift Solution


  • 0
    T
        func moveZeroes(inout nums: [Int]) {
            var zeroCount:Int = 0
            
            for index in 0..<nums.count {
                if nums[index] == 0 {
                    zeroCount++
                    continue
                }
                swap(&nums, index, index - zeroCount)
            }
        }
        
        func swap(inout nums: [Int], _ p:Int , _ q:Int) {
            if p <= (nums.count - 1) && q <= (nums.count - 1) {
                var num:Int = nums[q]
                nums[q] = nums[p]
                nums[p] = num
            }
        }
    

Log in to reply
 

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