forked from soapyigu/LeetCode-Swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProductExceptSelf.swift
46 lines (35 loc) · 1.17 KB
/
ProductExceptSelf.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
* Question Link: https://leetcode.com/problems/product-of-array-except-self/
* Primary idea: Use two arrays to hold multiplication result from left and right sides
* while iterating the original array
* Time Complexity: O(n), Space Complexity: O(n)
*/
class ProductExceptSelf {
func productExceptSelf(nums: [Int]) -> [Int] {
var res = [Int]()
guard nums.count > 0 else {
return res
}
let left = _initLeft(nums)
let right = _initRight(nums)
for i in 0 ..< nums.count {
res.append(left[i] * right[i])
}
return res
}
private func _initLeft(nums: [Int]) -> [Int] {
var left = [Int]()
left.append(1)
for i in 1 ..< nums.count {
left.append(left[i - 1] * nums[i - 1])
}
return left
}
private func _initRight(nums: [Int]) -> [Int] {
var right = Array(count: nums.count, repeatedValue: 1)
for i in (nums.count - 2).stride(through: 0, by: -1) {
right[i] = right[i + 1] * nums[i + 1]
}
return right
}
}