Skip to content

Commit

Permalink
[Array] Refactor solution to Move Zeros
Browse files Browse the repository at this point in the history
  • Loading branch information
soapyigu committed Mar 1, 2020
1 parent dd7df2e commit 06a0189
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions Array/MoveZeroes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,16 @@

class MoveZeroes {
func moveZeroes(_ nums: inout [Int]) {
var idx = 0
var nonZeroIdx = 0

for (i, num) in nums.enumerated() {
if num != 0 {
nums[idx] = num
idx += 1
}
for num in nums where num != 0 {
nums[nonZeroIdx] = num
nonZeroIdx += 1
}

while idx < nums.count {
nums[idx] = 0
idx += 1
while nonZeroIdx < nums.count {
nums[nonZeroIdx] = 0
nonZeroIdx += 1
}
}
}

0 comments on commit 06a0189

Please sign in to comment.