-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path238-product-of-array-except-self.cpp
35 lines (30 loc) · 1.36 KB
/
238-product-of-array-except-self.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
// Title: Product of Array Except Self
// Description:
// Given an array nums of n integers where n > 1, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].
// Constraint: It's guaranteed that the product of the elements of any prefix or suffix of the array (including the whole array) fits in a 32 bit integer.
// Note: Please solve it without division and in O(n).
// Follow up: Could you solve it with constant space complexity? (The output array does not count as extra space for the purpose of space complexity analysis.)
// Link: https://leetcode.com/problems/product-of-array-except-self/
// Time complexity: O(n)
// Space complexity: O(n)
class Solution {
public:
vector<int> productExceptSelf(vector<int> &nums) {
int n = nums.size();
vector<int> leftProduct(n);
vector<int> rightProduct(n);
for (int product = 1, i = 0; i < n; i++) {
leftProduct.at(i) = product;
product *= nums.at(i);
}
for (int product = 1, i = n-1; i >= 0; i--) {
rightProduct.at(i) = product;
product *= nums.at(i);
}
vector<int> result(n);
for (int i = 0; i < n; i++) {
result.at(i) = leftProduct.at(i) * rightProduct.at(i);
}
return result;
}
};