Skip to content

Commit 06a0189

Browse files
committed
[Array] Refactor solution to Move Zeros
1 parent dd7df2e commit 06a0189

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

Array/MoveZeroes.swift

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,16 @@
88

99
class MoveZeroes {
1010
func moveZeroes(_ nums: inout [Int]) {
11-
var idx = 0
11+
var nonZeroIdx = 0
1212

13-
for (i, num) in nums.enumerated() {
14-
if num != 0 {
15-
nums[idx] = num
16-
idx += 1
17-
}
13+
for num in nums where num != 0 {
14+
nums[nonZeroIdx] = num
15+
nonZeroIdx += 1
1816
}
1917

20-
while idx < nums.count {
21-
nums[idx] = 0
22-
idx += 1
18+
while nonZeroIdx < nums.count {
19+
nums[nonZeroIdx] = 0
20+
nonZeroIdx += 1
2321
}
2422
}
2523
}

0 commit comments

Comments
 (0)