Skip to content

Commit 1e87fb5

Browse files
committed
feat: update solutions to lc problem: No.0238
No.0238.Product of Array Except Self
1 parent 5b1560e commit 1e87fb5

File tree

14 files changed

+374
-181
lines changed

14 files changed

+374
-181
lines changed

solution/0100-0199/0172.Factorial Trailing Zeroes/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151

5252
<!-- 这里可写通用的实现逻辑 -->
5353

54-
题目实际上是求 1~n 中有多少个 5 个因数
54+
题目实际上是求 1~n 中有多少个 5 的因数
5555

5656
我们以 130 为例来分析:
5757

solution/0200-0299/0238.Product of Array Except Self/README.md

+32-35
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,15 @@
5656
class Solution:
5757
def productExceptSelf(self, nums: List[int]) -> List[int]:
5858
n = len(nums)
59-
output = [1 for _ in nums]
59+
ans = [1] * n
6060
left = right = 1
6161
for i in range(n):
62-
output[i] = left
62+
ans[i] = left
6363
left *= nums[i]
6464
for i in range(n - 1, -1, -1):
65-
output[i] *= right
65+
ans[i] *= right
6666
right *= nums[i]
67-
return output
67+
return ans
6868
```
6969

7070
### **Java**
@@ -75,16 +75,16 @@ class Solution:
7575
class Solution {
7676
public int[] productExceptSelf(int[] nums) {
7777
int n = nums.length;
78-
int[] output = new int[n];
78+
int[] ans = new int[n];
7979
for (int i = 0, left = 1; i < n; ++i) {
80-
output[i] = left;
80+
ans[i] = left;
8181
left *= nums[i];
8282
}
8383
for (int i = n - 1, right = 1; i >= 0; --i) {
84-
output[i] *= right;
84+
ans[i] *= right;
8585
right *= nums[i];
8686
}
87-
return output;
87+
return ans;
8888
}
8989
}
9090
```
@@ -98,32 +98,34 @@ class Solution {
9898
*/
9999
var productExceptSelf = function (nums) {
100100
const n = nums.length;
101-
let output = new Array(n);
101+
let ans = new Array(n);
102102
for (let i = 0, left = 1; i < n; ++i) {
103-
output[i] = left;
103+
ans[i] = left;
104104
left *= nums[i];
105105
}
106106
for (let i = n - 1, right = 1; i >= 0; --i) {
107-
output[i] *= right;
107+
ans[i] *= right;
108108
right *= nums[i];
109109
}
110-
return output;
110+
return ans;
111111
};
112112
```
113113

114114
### **TypeScript**
115115

116116
```ts
117117
function productExceptSelf(nums: number[]): number[] {
118-
let dpLeft = Array(nums.length).fill(1);
119-
let dpRight = Array(nums.length).fill(1);
120-
for (let i = 1; i < nums.length; i++) {
121-
dpLeft[i] = dpLeft[i - 1] * nums[i - 1];
118+
const n = nums.length;
119+
let ans = new Array(n);
120+
for (let i = 0, left = 1; i < n; ++i) {
121+
ans[i] = left;
122+
left *= nums[i];
122123
}
123-
for (let i = nums.length - 2; i >= 0; i--) {
124-
dpRight[i] = dpRight[i + 1] * nums[i + 1];
124+
for (let i = n - 1, right = 1; i >= 0; --i) {
125+
ans[i] *= right;
126+
right *= nums[i];
125127
}
126-
return dpLeft.map((x, i) => x * dpRight[i]);
128+
return ans;
127129
}
128130
```
129131

@@ -167,27 +169,22 @@ func productExceptSelf(nums []int) []int {
167169
### **C++**
168170

169171
```cpp
170-
class Solution
171-
{
172+
class Solution {
172173
public:
173-
vector<int> productExceptSelf(vector<int> &nums)
174-
{
175-
vector<int> dpLeft(nums.size(), 1);
176-
vector<int> dpRight(nums.size(), 1);
177-
for (int i = 1; i < nums.size(); i++)
178-
{
179-
dpLeft[i] = dpLeft[i - 1] * nums[i - 1];
180-
}
181-
for (int i = nums.size() - 2; i >= 0; i--)
174+
vector<int> productExceptSelf(vector<int>& nums) {
175+
int n = nums.size();
176+
vector<int> ans(n);
177+
for (int i = 0, left = 1; i < n; ++i)
182178
{
183-
dpRight[i] = dpRight[i + 1] * nums[i + 1];
179+
ans[i] = left;
180+
left *= nums[i];
184181
}
185-
vector<int> result;
186-
for (int i = 0; i < nums.size(); i++)
182+
for (int i = n - 1, right = 1; i >= 0; --i)
187183
{
188-
result.push_back(dpLeft[i] * dpRight[i]);
184+
ans[i] *= right;
185+
right *= nums[i];
189186
}
190-
return result;
187+
return ans;
191188
}
192189
};
193190
```

solution/0200-0299/0238.Product of Array Except Self/README_EN.md

+32-35
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@
4040
class Solution:
4141
def productExceptSelf(self, nums: List[int]) -> List[int]:
4242
n = len(nums)
43-
output = [1 for _ in nums]
43+
ans = [1] * n
4444
left = right = 1
4545
for i in range(n):
46-
output[i] = left
46+
ans[i] = left
4747
left *= nums[i]
4848
for i in range(n - 1, -1, -1):
49-
output[i] *= right
49+
ans[i] *= right
5050
right *= nums[i]
51-
return output
51+
return ans
5252
```
5353

5454
### **Java**
@@ -57,16 +57,16 @@ class Solution:
5757
class Solution {
5858
public int[] productExceptSelf(int[] nums) {
5959
int n = nums.length;
60-
int[] output = new int[n];
60+
int[] ans = new int[n];
6161
for (int i = 0, left = 1; i < n; ++i) {
62-
output[i] = left;
62+
ans[i] = left;
6363
left *= nums[i];
6464
}
6565
for (int i = n - 1, right = 1; i >= 0; --i) {
66-
output[i] *= right;
66+
ans[i] *= right;
6767
right *= nums[i];
6868
}
69-
return output;
69+
return ans;
7070
}
7171
}
7272
```
@@ -80,32 +80,34 @@ class Solution {
8080
*/
8181
var productExceptSelf = function (nums) {
8282
const n = nums.length;
83-
let output = new Array(n);
83+
let ans = new Array(n);
8484
for (let i = 0, left = 1; i < n; ++i) {
85-
output[i] = left;
85+
ans[i] = left;
8686
left *= nums[i];
8787
}
8888
for (let i = n - 1, right = 1; i >= 0; --i) {
89-
output[i] *= right;
89+
ans[i] *= right;
9090
right *= nums[i];
9191
}
92-
return output;
92+
return ans;
9393
};
9494
```
9595

9696
### **TypeScript**
9797

9898
```ts
9999
function productExceptSelf(nums: number[]): number[] {
100-
let dpLeft = Array(nums.length).fill(1);
101-
let dpRight = Array(nums.length).fill(1);
102-
for (let i = 1; i < nums.length; i++) {
103-
dpLeft[i] = dpLeft[i - 1] * nums[i - 1];
100+
const n = nums.length;
101+
let ans = new Array(n);
102+
for (let i = 0, left = 1; i < n; ++i) {
103+
ans[i] = left;
104+
left *= nums[i];
104105
}
105-
for (let i = nums.length - 2; i >= 0; i--) {
106-
dpRight[i] = dpRight[i + 1] * nums[i + 1];
106+
for (let i = n - 1, right = 1; i >= 0; --i) {
107+
ans[i] *= right;
108+
right *= nums[i];
107109
}
108-
return dpLeft.map((x, i) => x * dpRight[i]);
110+
return ans;
109111
}
110112
```
111113

@@ -147,27 +149,22 @@ func productExceptSelf(nums []int) []int {
147149
### **C++**
148150

149151
```cpp
150-
class Solution
151-
{
152+
class Solution {
152153
public:
153-
vector<int> productExceptSelf(vector<int> &nums)
154-
{
155-
vector<int> dpLeft(nums.size(), 1);
156-
vector<int> dpRight(nums.size(), 1);
157-
for (int i = 1; i < nums.size(); i++)
158-
{
159-
dpLeft[i] = dpLeft[i - 1] * nums[i - 1];
160-
}
161-
for (int i = nums.size() - 2; i >= 0; i--)
154+
vector<int> productExceptSelf(vector<int>& nums) {
155+
int n = nums.size();
156+
vector<int> ans(n);
157+
for (int i = 0, left = 1; i < n; ++i)
162158
{
163-
dpRight[i] = dpRight[i + 1] * nums[i + 1];
159+
ans[i] = left;
160+
left *= nums[i];
164161
}
165-
vector<int> result;
166-
for (int i = 0; i < nums.size(); i++)
162+
for (int i = n - 1, right = 1; i >= 0; --i)
167163
{
168-
result.push_back(dpLeft[i] * dpRight[i]);
164+
ans[i] *= right;
165+
right *= nums[i];
169166
}
170-
return result;
167+
return ans;
171168
}
172169
};
173170
```
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,18 @@
1-
class Solution
2-
{
1+
class Solution {
32
public:
4-
vector<int> productExceptSelf(vector<int> &nums)
5-
{
6-
vector<int> dpLeft(nums.size(), 1);
7-
vector<int> dpRight(nums.size(), 1);
8-
for (int i = 1; i < nums.size(); i++)
3+
vector<int> productExceptSelf(vector<int>& nums) {
4+
int n = nums.size();
5+
vector<int> ans(n);
6+
for (int i = 0, left = 1; i < n; ++i)
97
{
10-
dpLeft[i] = dpLeft[i - 1] * nums[i - 1];
8+
ans[i] = left;
9+
left *= nums[i];
1110
}
12-
for (int i = nums.size() - 2; i >= 0; i--)
11+
for (int i = n - 1, right = 1; i >= 0; --i)
1312
{
14-
dpRight[i] = dpRight[i + 1] * nums[i + 1];
13+
ans[i] *= right;
14+
right *= nums[i];
1515
}
16-
vector<int> result;
17-
for (int i = 0; i < nums.size(); i++)
18-
{
19-
result.push_back(dpLeft[i] * dpRight[i]);
20-
}
21-
return result;
16+
return ans;
2217
}
2318
};
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
class Solution {
2-
public int[] productExceptSelf(int[] nums) {
3-
int n = nums.length;
4-
int[] output = new int[n];
5-
for (int i = 0, left = 1; i < n; ++i) {
6-
output[i] = left;
7-
left *= nums[i];
8-
}
9-
for (int i = n - 1, right = 1; i >= 0; --i) {
10-
output[i] *= right;
11-
right *= nums[i];
12-
}
13-
return output;
14-
}
1+
class Solution {
2+
public int[] productExceptSelf(int[] nums) {
3+
int n = nums.length;
4+
int[] ans = new int[n];
5+
for (int i = 0, left = 1; i < n; ++i) {
6+
ans[i] = left;
7+
left *= nums[i];
8+
}
9+
for (int i = n - 1, right = 1; i >= 0; --i) {
10+
ans[i] *= right;
11+
right *= nums[i];
12+
}
13+
return ans;
14+
}
1515
}

solution/0200-0299/0238.Product of Array Except Self/Solution.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
*/
55
var productExceptSelf = function (nums) {
66
const n = nums.length;
7-
let output = new Array(n);
7+
let ans = new Array(n);
88
for (let i = 0, left = 1; i < n; ++i) {
9-
output[i] = left;
9+
ans[i] = left;
1010
left *= nums[i];
1111
}
1212
for (let i = n - 1, right = 1; i >= 0; --i) {
13-
output[i] *= right;
13+
ans[i] *= right;
1414
right *= nums[i];
1515
}
16-
return output;
16+
return ans;
1717
};
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
class Solution:
2-
def productExceptSelf(self, nums: List[int]) -> List[int]:
3-
n = len(nums)
4-
output = [1 for _ in nums]
5-
left = right = 1
6-
for i in range(n):
7-
output[i] = left
8-
left *= nums[i]
9-
for i in range(n - 1, -1, -1):
10-
output[i] *= right
11-
right *= nums[i]
12-
return output
1+
class Solution:
2+
def productExceptSelf(self, nums: List[int]) -> List[int]:
3+
n = len(nums)
4+
ans = [1] * n
5+
left = right = 1
6+
for i in range(n):
7+
ans[i] = left
8+
left *= nums[i]
9+
for i in range(n - 1, -1, -1):
10+
ans[i] *= right
11+
right *= nums[i]
12+
return ans
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
function productExceptSelf(nums: number[]): number[] {
2-
let dpLeft = Array(nums.length).fill(1);
3-
let dpRight = Array(nums.length).fill(1);
4-
for (let i = 1; i < nums.length; i++) {
5-
dpLeft[i] = dpLeft[i - 1] * nums[i - 1];
2+
const n = nums.length;
3+
let ans = new Array(n);
4+
for (let i = 0, left = 1; i < n; ++i) {
5+
ans[i] = left;
6+
left *= nums[i];
67
}
7-
for (let i = nums.length - 2; i >= 0; i--) {
8-
dpRight[i] = dpRight[i + 1] * nums[i + 1];
8+
for (let i = n - 1, right = 1; i >= 0; --i) {
9+
ans[i] *= right;
10+
right *= nums[i];
911
}
10-
return dpLeft.map((x, i) => x * dpRight[i]);
12+
return ans;
1113
}

0 commit comments

Comments
 (0)