Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 47 additions & 1 deletion solution/1300-1399/1352.Product of the Last K Numbers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ productOfNumbers.getProduct(2); // 返回 20 。最后 2 个数字的乘积是 5
productOfNumbers.getProduct(3); // 返回 40 。最后 3 个数字的乘积是 2 * 5 * 4 = 40
productOfNumbers.getProduct(4); // 返回 0 。最后 4 个数字的乘积是 0 * 2 * 5 * 4 = 0
productOfNumbers.add(8); // [3,0,2,5,4,8]
productOfNumbers.getProduct(2); // 返回 32 。最后 2 个数字的乘积是 4 * 8 = 32
productOfNumbers.getProduct(2); // 返回 32 。最后 2 个数字的乘积是 4 * 8 = 32
</pre>

<p>&nbsp;</p>
Expand Down Expand Up @@ -218,6 +218,52 @@ func (this *ProductOfNumbers) GetProduct(k int) int {
*/
```

#### TypeScript

```ts
class ProductOfNumbers {
s = [1];

add(num: number): void {
if (num === 0) {
this.s = [1];
} else {
const i = this.s.length;
this.s[i] = this.s[i - 1] * num;
}
}

getProduct(k: number): number {
const i = this.s.length;
if (k > i - 1) return 0;
return this.s[i - 1] / this.s[i - k - 1];
}
}
```

#### JavaScript

```js
class ProductOfNumbers {
s = [1];

add(num) {
if (num === 0) {
this.s = [1];
} else {
const i = this.s.length;
this.s[i] = this.s[i - 1] * num;
}
}

getProduct(k) {
const i = this.s.length;
if (k > i - 1) return 0;
return this.s[i - 1] / this.s[i - k - 1];
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ productOfNumbers.getProduct(2); // return 20. The product of the last 2 numbers
productOfNumbers.getProduct(3); // return 40. The product of the last 3 numbers is 2 * 5 * 4 = 40
productOfNumbers.getProduct(4); // return 0. The product of the last 4 numbers is 0 * 2 * 5 * 4 = 0
productOfNumbers.add(8); // [3,0,2,5,4,8]
productOfNumbers.getProduct(2); // return 32. The product of the last 2 numbers is 4 * 8 = 32
productOfNumbers.getProduct(2); // return 32. The product of the last 2 numbers is 4 * 8 = 32
</pre>

<p>&nbsp;</p>
Expand Down Expand Up @@ -213,6 +213,52 @@ func (this *ProductOfNumbers) GetProduct(k int) int {
*/
```

#### TypeScript

```ts
class ProductOfNumbers {
s = [1];

add(num: number): void {
if (num === 0) {
this.s = [1];
} else {
const i = this.s.length;
this.s[i] = this.s[i - 1] * num;
}
}

getProduct(k: number): number {
const i = this.s.length;
if (k > i - 1) return 0;
return this.s[i - 1] / this.s[i - k - 1];
}
}
```

#### JavaScript

```js
class ProductOfNumbers {
s = [1];

add(num) {
if (num === 0) {
this.s = [1];
} else {
const i = this.s.length;
this.s[i] = this.s[i - 1] * num;
}
}

getProduct(k) {
const i = this.s.length;
if (k > i - 1) return 0;
return this.s[i - 1] / this.s[i - k - 1];
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
18 changes: 18 additions & 0 deletions solution/1300-1399/1352.Product of the Last K Numbers/Solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class ProductOfNumbers {
s = [1];

add(num) {
if (num === 0) {
this.s = [1];
} else {
const i = this.s.length;
this.s[i] = this.s[i - 1] * num;
}
}

getProduct(k) {
const i = this.s.length;
if (k > i - 1) return 0;
return this.s[i - 1] / this.s[i - k - 1];
}
}
18 changes: 18 additions & 0 deletions solution/1300-1399/1352.Product of the Last K Numbers/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class ProductOfNumbers {
s = [1];

add(num: number): void {
if (num === 0) {
this.s = [1];
} else {
const i = this.s.length;
this.s[i] = this.s[i - 1] * num;
}
}

getProduct(k: number): number {
const i = this.s.length;
if (k > i - 1) return 0;
return this.s[i - 1] / this.s[i - k - 1];
}
}