-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path238.ProductofArrayExceptSelf.h
More file actions
129 lines (94 loc) · 2.88 KB
/
238.ProductofArrayExceptSelf.h
File metadata and controls
129 lines (94 loc) · 2.88 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
Given an array of n integers where n > 1, nums, return an array output such that output[i]
is equal to the product of all the elements of nums except nums[i].
Solve it without division and in O(n).
For example, given [1,2,3,4], return [24,12,8,6].
https://leetcode.com/problems/product-of-array-except-self/
*/
/* 2018-10-22,
vector<int> productExceptSelf(vector<int>& nums) {
int n = nums.size();
if (n < 1) return nums;
vector<int> left(n, 0);
vector<int> right(n, 0);
vector<int> res(n, 0);
left[0] = nums[0];
for (int i = 1; i < n; i++) {
left[i] = left[i-1] * nums[i];
}
right[n-1] = nums[n-1];
for (int i = n-2; i > 0; i--) {
right[i] = right[i+1] * nums[i];
}
res[0] = right[1];
res[n-1] = left[n-2];
for (int i = 1; i < n-1; i++) {
res[i] = left[i-1] * right[i+1];
}
return res;
}
/* 2017-01-01, update */
vector<int> productExceptSelf(vector<int>& nums) {
vector<int> res(nums.size(), 1);
for (int i = 1; i < nums.size(); ++i) {
res[i] = res[i - 1] * nums[i - 1];
}
int right = 1;
for (int i = nums.size() - 1; i >= 0; --i) {
res[i] *= right;
right *= nums[i];
}
return res;
}
/* 2016-09-02, update */
vector<int> productExceptSelf(vector<int>& nums) {
int n = nums.size();
vector<int> vec(n, 1);
int p = 1;
for (int i = 1; i < n; i++)
{
p = p * nums[i-1];
vec[i] = p;
}
p = 1;
for (int i = n-2; i >= 0; i--)
{
p = p * nums[i+1];
vec[i] *= p;
}
return vec;
}
vector<int> productExceptSelf(vector<int>& nums) {
int n = nums.size();
vector<int> vec(n, 0);
vec[0] = 1;
int p = 1;
for (int i = 1; i < n; i++)
{
p = p * nums[i-1];
vec[i] = p;
}
p = 1;
for (int i = n-2; i >= 0; i--)
{
p = p * nums[i+1];
vec[i] *= p;
}
return vec;
}
/* recursion */
/* http://blog.csdn.net/wzy_1988/article/details/46916179 */
public int[] productExceptSelfRev(int[] nums) {
multiply(nums, 1, 0, nums.length);
return nums;
}
private int multiply(int[] a, int fwdProduct, int indx, int N) {
int revProduct = 1;
if (indx < N) {
revProduct = multiply(a, fwdProduct * a[indx], indx + 1, N);
int cur = a[indx];
a[indx] = fwdProduct * revProduct;
revProduct *= cur;
}
return revProduct;
}