Skip to content

Commit 06a3f3d

Browse files
committed
solve product of array except self
1 parent ef0b325 commit 06a3f3d

File tree

1 file changed

+17
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)