-
Notifications
You must be signed in to change notification settings - Fork 0
/
200524-2.cpp
54 lines (51 loc) · 919 Bytes
/
200524-2.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
// https://leetcode-cn.com/problems/product-of-array-except-self/
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
vector<int> productExceptSelf(vector<int>& nums) {
int n = nums.size();
vector<int> a(n);
for (int i = 0; i < n; ++i) {
if (i == 0) {
a[i] = nums[i];
} else {
a[i] = a[i - 1] * nums[i];
}
}
vector<int> b(n);
for (int i = n - 1; i >= 0; --i) {
if (i == n - 1) {
b[i] = nums[i];
} else {
b[i] = b[i + 1] * nums[i];
}
}
vector<int> c(n);
for (int i = 0; i < n; ++i) {
if (i == 0) {
c[i] = b[i + 1];
} else if (i == n - 1) {
c[i] = a[i - 1];
} else {
c[i] = a[i - 1] * b[i + 1];
}
}
return c;
}
};
void print(vector<int>&& a)
{
for (auto e : a) cout << e << " ";
cout << endl;
}
int main()
{
Solution s;
{
vector<int> a = {1,2,3,4};
print(s.productExceptSelf(a));
}
return 0;
}