Skip to content

Commit 24c25fd

Browse files
authored
feat: add solutions to lc problem: No.1352 (#4063)
1 parent 3cdcd89 commit 24c25fd

File tree

4 files changed

+130
-2
lines changed

4 files changed

+130
-2
lines changed

solution/1300-1399/1352.Product of the Last K Numbers/README.md

+47-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ productOfNumbers.getProduct(2); // 返回 20 。最后 2 个数字的乘积是 5
6161
productOfNumbers.getProduct(3); // 返回 40 。最后 3 个数字的乘积是 2 * 5 * 4 = 40
6262
productOfNumbers.getProduct(4); // 返回 0 。最后 4 个数字的乘积是 0 * 2 * 5 * 4 = 0
6363
productOfNumbers.add(8); // [3,0,2,5,4,8]
64-
productOfNumbers.getProduct(2); // 返回 32 。最后 2 个数字的乘积是 4 * 8 = 32
64+
productOfNumbers.getProduct(2); // 返回 32 。最后 2 个数字的乘积是 4 * 8 = 32
6565
</pre>
6666

6767
<p>&nbsp;</p>
@@ -218,6 +218,52 @@ func (this *ProductOfNumbers) GetProduct(k int) int {
218218
*/
219219
```
220220

221+
#### TypeScript
222+
223+
```ts
224+
class ProductOfNumbers {
225+
s = [1];
226+
227+
add(num: number): void {
228+
if (num === 0) {
229+
this.s = [1];
230+
} else {
231+
const i = this.s.length;
232+
this.s[i] = this.s[i - 1] * num;
233+
}
234+
}
235+
236+
getProduct(k: number): number {
237+
const i = this.s.length;
238+
if (k > i - 1) return 0;
239+
return this.s[i - 1] / this.s[i - k - 1];
240+
}
241+
}
242+
```
243+
244+
#### JavaScript
245+
246+
```js
247+
class ProductOfNumbers {
248+
s = [1];
249+
250+
add(num) {
251+
if (num === 0) {
252+
this.s = [1];
253+
} else {
254+
const i = this.s.length;
255+
this.s[i] = this.s[i - 1] * num;
256+
}
257+
}
258+
259+
getProduct(k) {
260+
const i = this.s.length;
261+
if (k > i - 1) return 0;
262+
return this.s[i - 1] / this.s[i - k - 1];
263+
}
264+
}
265+
```
266+
221267
<!-- tabs:end -->
222268

223269
<!-- solution:end -->

solution/1300-1399/1352.Product of the Last K Numbers/README_EN.md

+47-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ productOfNumbers.getProduct(2); // return 20. The product of the last 2 numbers
5656
productOfNumbers.getProduct(3); // return 40. The product of the last 3 numbers is 2 * 5 * 4 = 40
5757
productOfNumbers.getProduct(4); // return 0. The product of the last 4 numbers is 0 * 2 * 5 * 4 = 0
5858
productOfNumbers.add(8); // [3,0,2,5,4,8]
59-
productOfNumbers.getProduct(2); // return 32. The product of the last 2 numbers is 4 * 8 = 32
59+
productOfNumbers.getProduct(2); // return 32. The product of the last 2 numbers is 4 * 8 = 32
6060
</pre>
6161

6262
<p>&nbsp;</p>
@@ -213,6 +213,52 @@ func (this *ProductOfNumbers) GetProduct(k int) int {
213213
*/
214214
```
215215

216+
#### TypeScript
217+
218+
```ts
219+
class ProductOfNumbers {
220+
s = [1];
221+
222+
add(num: number): void {
223+
if (num === 0) {
224+
this.s = [1];
225+
} else {
226+
const i = this.s.length;
227+
this.s[i] = this.s[i - 1] * num;
228+
}
229+
}
230+
231+
getProduct(k: number): number {
232+
const i = this.s.length;
233+
if (k > i - 1) return 0;
234+
return this.s[i - 1] / this.s[i - k - 1];
235+
}
236+
}
237+
```
238+
239+
#### JavaScript
240+
241+
```js
242+
class ProductOfNumbers {
243+
s = [1];
244+
245+
add(num) {
246+
if (num === 0) {
247+
this.s = [1];
248+
} else {
249+
const i = this.s.length;
250+
this.s[i] = this.s[i - 1] * num;
251+
}
252+
}
253+
254+
getProduct(k) {
255+
const i = this.s.length;
256+
if (k > i - 1) return 0;
257+
return this.s[i - 1] / this.s[i - k - 1];
258+
}
259+
}
260+
```
261+
216262
<!-- tabs:end -->
217263

218264
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class ProductOfNumbers {
2+
s = [1];
3+
4+
add(num) {
5+
if (num === 0) {
6+
this.s = [1];
7+
} else {
8+
const i = this.s.length;
9+
this.s[i] = this.s[i - 1] * num;
10+
}
11+
}
12+
13+
getProduct(k) {
14+
const i = this.s.length;
15+
if (k > i - 1) return 0;
16+
return this.s[i - 1] / this.s[i - k - 1];
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class ProductOfNumbers {
2+
s = [1];
3+
4+
add(num: number): void {
5+
if (num === 0) {
6+
this.s = [1];
7+
} else {
8+
const i = this.s.length;
9+
this.s[i] = this.s[i - 1] * num;
10+
}
11+
}
12+
13+
getProduct(k: number): number {
14+
const i = this.s.length;
15+
if (k > i - 1) return 0;
16+
return this.s[i - 1] / this.s[i - k - 1];
17+
}
18+
}

0 commit comments

Comments
 (0)