238. 除自身以外数组的乘积
为保证权益,题目请参考 238. 除自身以外数组的乘积(From LeetCode).
解决方案1
CPP
C++
/*
LeetCode 238
*/
#include <iostream>
#include <vector>
using namespace std;
class Solution
{
public:
vector<int> productExceptSelf(vector<int> &nums)
{
vector<int> res(nums.size(), 1);
int tmp = 1;
for (int i = 0; i < nums.size(); ++i)
{
res[i] *= tmp;
tmp *= nums[i];
}
tmp = 1;
for (int i = nums.size() - 1; i >= 0; --i)
{
res[i] *= tmp;
tmp *= nums[i];
}
return res;
}
};
int main()
{
return 0;
}
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
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