-
Notifications
You must be signed in to change notification settings - Fork 257
/
product-of-array-exclude-itself.cpp
65 lines (54 loc) · 1.75 KB
/
product-of-array-exclude-itself.cpp
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Time: O(n)
// Space: O(1)
class Solution {
/**
* @param A: Given an integers array A
* @return: A long long array B and B[i]= A[0] * ... * A[i-1] * A[i+1] * ... * A[n-1]
*/
public:
vector<long long> productExcludeItself(vector<int> &nums) {
if (nums.empty()) {
return {};
}
vector<long long> left_product(nums.size());
left_product[0] = 1;
for (int i = 1; i < nums.size(); ++i) {
left_product[i] = left_product[i - 1] * nums[i - 1];
}
long long right_product = 1;
for (int i = static_cast<int>(nums.size()) - 2; i >= 0; --i) {
right_product *= nums[i + 1];
left_product[i] = left_product[i] * right_product;
}
return left_product;
}
};
// Time: O(n)
// Space: O(n)
class Solution2 {
public:
/**
* @param A: Given an integers array A
* @return: A long long array B and B[i]= A[0] * ... * A[i-1] * A[i+1] * ... * A[n-1]
*/
vector<long long> productExcludeItself(vector<int> &nums) {
if (nums.empty()) {
return {};
}
vector<long long> left_product(nums.size());
vector<long long> right_product(nums.size());
vector<long long> product(nums.size());
left_product[0] = 1;
for (int i = 1; i < nums.size(); ++i) {
left_product[i] = left_product[i - 1] * nums[i - 1];
}
right_product[nums.size() - 1] = 1;
for (int j = static_cast<int>(nums.size()) - 2; j >= 0; --j) {
right_product[j] = right_product[j + 1] * nums[j + 1];
}
for (int k = 0; k < nums.size(); ++k) {
product[k] = left_product[k] * right_product[k];
}
return product;
}
};