Skip to content

Commit 7fff63d

Browse files
committed
Solution Product of Array Except Self
1 parent 942ff43 commit 7fff63d

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
func productExceptSelf(_ nums: [Int]) -> [Int] {
3+
let length = nums.count
4+
var result = Array(repeating: 1, count: length)
5+
var leftProduct = 1
6+
for i in 0..<length {
7+
result[i] = leftProduct
8+
leftProduct *= nums[i]
9+
}
10+
11+
var rightProduct = 1
12+
for i in (0..<length).reversed() {
13+
result[i] *= rightProduct
14+
rightProduct *= nums[i]
15+
}
16+
17+
return result
18+
}
19+
}

0 commit comments

Comments
 (0)