Skip to content

Commit 7b1e638

Browse files
committed
fix: show solutions
No.0238.Product of Array Except Self
1 parent 058dd4d commit 7b1e638

File tree

4 files changed

+165
-29
lines changed

4 files changed

+165
-29
lines changed

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

+66
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,22 @@ var productExceptSelf = function (nums) {
111111
};
112112
```
113113

114+
### **TypeScript**
115+
116+
```ts
117+
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];
122+
}
123+
for (let i = nums.length - 2; i >= 0; i--) {
124+
dpRight[i] = dpRight[i + 1] * nums[i + 1];
125+
}
126+
return dpLeft.map((x, i) => x * dpRight[i]);
127+
}
128+
```
129+
114130
### **Go**
115131

116132
利用前缀和思想,分别求出 `i` 左右两侧的乘积
@@ -140,6 +156,56 @@ func productExceptSelf(nums []int) []int {
140156
}
141157
```
142158

159+
### **C++**
160+
161+
```cpp
162+
class Solution
163+
{
164+
public:
165+
vector<int> productExceptSelf(vector<int> &nums)
166+
{
167+
vector<int> dpLeft(nums.size(), 1);
168+
vector<int> dpRight(nums.size(), 1);
169+
for (int i = 1; i < nums.size(); i++)
170+
{
171+
dpLeft[i] = dpLeft[i - 1] * nums[i - 1];
172+
}
173+
for (int i = nums.size() - 2; i >= 0; i--)
174+
{
175+
dpRight[i] = dpRight[i + 1] * nums[i + 1];
176+
}
177+
vector<int> result;
178+
for (int i = 0; i < nums.size(); i++)
179+
{
180+
result.push_back(dpLeft[i] * dpRight[i]);
181+
}
182+
return result;
183+
}
184+
};
185+
```
186+
187+
### **Rust**
188+
189+
```rust
190+
impl Solution {
191+
pub fn product_except_self(nums: Vec<i32>) -> Vec<i32> {
192+
let mut dp_left = vec![1_i32; nums.len()];
193+
let mut dp_right = vec![1_i32; nums.len()];
194+
for i in 1..nums.len() {
195+
dp_left[i] = dp_left[i - 1] * nums[i - 1];
196+
}
197+
for i in (0..(nums.len() - 1)).rev() {
198+
dp_right[i] = dp_right[i + 1] * nums[i + 1];
199+
}
200+
dp_left
201+
.into_iter()
202+
.enumerate()
203+
.map(|(i, x)| x * dp_right[i])
204+
.collect()
205+
}
206+
}
207+
```
208+
143209
### **...**
144210

145211
```

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

+66
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,22 @@ var productExceptSelf = function (nums) {
9393
};
9494
```
9595

96+
### **TypeScript**
97+
98+
```ts
99+
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];
104+
}
105+
for (let i = nums.length - 2; i >= 0; i--) {
106+
dpRight[i] = dpRight[i + 1] * nums[i + 1];
107+
}
108+
return dpLeft.map((x, i) => x * dpRight[i]);
109+
}
110+
```
111+
96112
### **Go**
97113

98114
```go
@@ -120,6 +136,56 @@ func productExceptSelf(nums []int) []int {
120136
}
121137
```
122138

139+
### **C++**
140+
141+
```cpp
142+
class Solution
143+
{
144+
public:
145+
vector<int> productExceptSelf(vector<int> &nums)
146+
{
147+
vector<int> dpLeft(nums.size(), 1);
148+
vector<int> dpRight(nums.size(), 1);
149+
for (int i = 1; i < nums.size(); i++)
150+
{
151+
dpLeft[i] = dpLeft[i - 1] * nums[i - 1];
152+
}
153+
for (int i = nums.size() - 2; i >= 0; i--)
154+
{
155+
dpRight[i] = dpRight[i + 1] * nums[i + 1];
156+
}
157+
vector<int> result;
158+
for (int i = 0; i < nums.size(); i++)
159+
{
160+
result.push_back(dpLeft[i] * dpRight[i]);
161+
}
162+
return result;
163+
}
164+
};
165+
```
166+
167+
### **Rust**
168+
169+
```rust
170+
impl Solution {
171+
pub fn product_except_self(nums: Vec<i32>) -> Vec<i32> {
172+
let mut dp_left = vec![1_i32; nums.len()];
173+
let mut dp_right = vec![1_i32; nums.len()];
174+
for i in 1..nums.len() {
175+
dp_left[i] = dp_left[i - 1] * nums[i - 1];
176+
}
177+
for i in (0..(nums.len() - 1)).rev() {
178+
dp_right[i] = dp_right[i + 1] * nums[i + 1];
179+
}
180+
dp_left
181+
.into_iter()
182+
.enumerate()
183+
.map(|(i, x)| x * dp_right[i])
184+
.collect()
185+
}
186+
}
187+
```
188+
123189
### **...**
124190

125191
```
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
class Solution
22
{
33
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++)
4+
vector<int> productExceptSelf(vector<int> &nums)
95
{
10-
dpLeft[i] = dpLeft[i - 1] * nums[i - 1];
6+
vector<int> dpLeft(nums.size(), 1);
7+
vector<int> dpRight(nums.size(), 1);
8+
for (int i = 1; i < nums.size(); i++)
9+
{
10+
dpLeft[i] = dpLeft[i - 1] * nums[i - 1];
11+
}
12+
for (int i = nums.size() - 2; i >= 0; i--)
13+
{
14+
dpRight[i] = dpRight[i + 1] * nums[i + 1];
15+
}
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;
1122
}
12-
for (int i = nums.size() - 2; i >= 0; i--)
13-
{
14-
dpRight[i] = dpRight[i + 1] * nums[i + 1];
15-
}
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;
22-
}
2323
};
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
impl Solution {
2-
pub fn product_except_self(nums: Vec<i32>) -> Vec<i32> {
3-
let mut dp_left=vec![1_i32;nums.len()];
4-
let mut dp_right=vec![1_i32;nums.len()];
5-
for i in 1..nums.len(){
6-
dp_left[i]=dp_left[i-1]*nums[i-1];
7-
}
8-
for i in (0..(nums.len()-1)).rev(){
9-
dp_right[i]=dp_right[i+1]*nums[i+1];
10-
}
11-
dp_left.into_iter().enumerate().map(|(i,x)| x*dp_right[i]).collect()
12-
}
13-
}
2+
pub fn product_except_self(nums: Vec<i32>) -> Vec<i32> {
3+
let mut dp_left = vec![1_i32; nums.len()];
4+
let mut dp_right = vec![1_i32; nums.len()];
5+
for i in 1..nums.len() {
6+
dp_left[i] = dp_left[i - 1] * nums[i - 1];
7+
}
8+
for i in (0..(nums.len() - 1)).rev() {
9+
dp_right[i] = dp_right[i + 1] * nums[i + 1];
10+
}
11+
dp_left
12+
.into_iter()
13+
.enumerate()
14+
.map(|(i, x)| x * dp_right[i])
15+
.collect()
16+
}
17+
}

0 commit comments

Comments
 (0)