forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0238.cpp
More file actions
31 lines (29 loc) · 699 Bytes
/
0238.cpp
File metadata and controls
31 lines (29 loc) · 699 Bytes
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
#include <iostream>
#include <vector>
using namespace std;
static int x = []() {std::ios::sync_with_stdio(false); cin.tie(0); return 0; }();
class Solution
{
public:
vector<int> productExceptSelf(vector<int>& nums)
{
vector<int> result(nums.size(), 1);
for (unsigned int i = 0; i < nums.size()-1; ++i)
{
result[i+1] = result[i]*nums[i];
}
int right = 1;
for (int i = nums.size() - 1; i >= 0; --i)
{
result[i] *= right;
right *= nums[i];
}
return result;
}
};
int main()
{
vector<int> nums = {1,2,3,4};
auto result = Solution().productExceptSelf(nums);
return 0;
}