Skip to content

Commit c579477

Browse files
committed
238 Product of Array Except Self
1 parent bb2eb28 commit c579477

File tree

3 files changed

+129
-0
lines changed

3 files changed

+129
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@
132132
+ [235 Lowest Common Ancestor of a Binary Search Tree(LCA, 最低公共祖先,二叉搜索树)](algorithms/LowestCommonAncestorofaBinarySearchTree)
133133
+ [236 Lowest Common Ancestor of a Binary Tree(LCA,最低公共祖先,二叉树](algorithms/LowestCommonAncestorofaBinaryTree)
134134
+ [237 Delete Node in a Linked List(O(1)删除单链表节点)](algorithms/DeleteNodeinaLinkedList)
135+
+ [238 Product of Array Except Self(数组操作)](algorithms/ProductofArrayExceptSelf)
135136

136137
## Database
137138

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
## Product of Array Except Self
2+
3+
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]`.
4+
5+
Solve it without division and in O(n).
6+
7+
For example, given `[1,2,3,4]`, return `[24,12,8,6]`.
8+
9+
Follow up:
10+
Could you solve it with constant space complexity? (Note: The output array does not count as extra space for the purpose of space complexity analysis.)
11+
12+
## Solution
13+
14+
简单方法是使用两个数组a,b,其中一个存储`nums[0..i - 1]`, 另一个数组存`nums[i + 1..n-1]`, 于是结果为`result[i] = a[i] * b[n - i - 1]`,即
15+
a存储i前面的积,b存储i后面元素的积
16+
17+
```cpp
18+
vector<int> productExceptSelf(vector<int>& nums) {
19+
if (nums.empty()) // 空
20+
return vector<int>();
21+
if (nums.size() == 1) { //只有一个元素,返回{0}
22+
return vector<int>({0});
23+
}
24+
auto n = nums.size();
25+
vector<int> a(n, 1), b(n, 1);
26+
for (auto i = 1; i < n; ++i) {
27+
a[i] = a[i - 1] * nums[i - 1];
28+
b[i] = b[i - 1] * nums[n - i];
29+
}
30+
for (auto i = 0; i < n; ++i) {
31+
a[i] *= b[n - i - 1];
32+
}
33+
return a;
34+
}
35+
```
36+
37+
以上方法O(n)时间,除了结果空间,还需要额外空间O(n)
38+
39+
题目要求O(1)空间。
40+
41+
我们发现a数组得到i前面的积后,只需要累乘后面的积即可。
42+
43+
* `a[n- 1]`不需要乘
44+
* `a[n - 2] = a[n - 2] * nums[n - 1]`
45+
* `a[n - 3] = a[n - 3] * nums[n - 1] * nums[n - 2]`
46+
* ...
47+
* `a[0] = a[0] * nums[n - 1] * nums[n - 2] * ... * nums[1]`
48+
49+
使用一个变量product,表示从后往前的累乘,则`a[i] = a[i] * product`
50+
51+
```cpp
52+
vector<int> productExceptSelf(vector<int> &nums) {
53+
if (nums.empty()) // 空
54+
return vector<int>();
55+
if (nums.size() == 1) { //只有一个元素,返回{0}
56+
return vector<int>({0});
57+
}
58+
int n = nums.size();
59+
vector<int> result(n, 1);
60+
for (int i = 1; i < n; ++i)
61+
result[i] = result[i - 1] * nums[i - 1];
62+
int product = 1;
63+
for (int i = n - 2; i >= 0; --i) {
64+
product *= nums[i + 1];
65+
result[i] *= product;
66+
}
67+
return result;
68+
69+
}
70+
```
71+
此时空间为O(1)
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include <vector>
2+
#include <iostream>
3+
#include <cstdio>
4+
#include <algorithm>
5+
using namespace std;
6+
static inline void print(const vector<int> &a) {
7+
for_each(begin(a), end(a), [](int i){cout << i << ' ';});
8+
cout << endl;
9+
}
10+
class Solution {
11+
public:
12+
/*
13+
vector<int> productExceptSelf(vector<int>& nums) {
14+
if (nums.empty()) // 空
15+
return vector<int>();
16+
if (nums.size() == 1) { //只有一个元素,返回{0}
17+
return vector<int>({0});
18+
}
19+
auto n = nums.size();
20+
vector<int> a(n, 1), b(n, 1);
21+
for (auto i = 1; i < n; ++i) {
22+
a[i] = a[i - 1] * nums[i - 1];
23+
b[i] = b[i - 1] * nums[n - i];
24+
}
25+
for (auto i = 0; i < n; ++i) {
26+
a[i] *= b[n - i - 1];
27+
}
28+
return a;
29+
}
30+
*/
31+
vector<int> productExceptSelf(vector<int> &nums) {
32+
if (nums.empty()) //
33+
return vector<int>();
34+
if (nums.size() == 1) { //只有一个元素,返回{0}
35+
return vector<int>({0});
36+
}
37+
int n = nums.size();
38+
vector<int> result(n, 1);
39+
for (int i = 1; i < n; ++i)
40+
result[i] = result[i - 1] * nums[i - 1];
41+
int product = 1;
42+
for (int i = n - 2; i >= 0; --i) {
43+
product *= nums[i + 1];
44+
result[i] *= product;
45+
}
46+
return result;
47+
48+
}
49+
};
50+
int main(int argc, char **argv)
51+
{
52+
Solution solution;
53+
vector<int> v = {1,2,3,4};
54+
auto result = solution.productExceptSelf(v);
55+
print(result);
56+
return 0;
57+
}

0 commit comments

Comments
 (0)