diff --git a/solution/1300-1399/1352.Product of the Last K Numbers/README.md b/solution/1300-1399/1352.Product of the Last K Numbers/README.md index 4db8ec50691f2..a18cf267c0c0d 100644 --- a/solution/1300-1399/1352.Product of the Last K Numbers/README.md +++ b/solution/1300-1399/1352.Product of the Last K Numbers/README.md @@ -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
@@ -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]; + } +} +``` + diff --git a/solution/1300-1399/1352.Product of the Last K Numbers/README_EN.md b/solution/1300-1399/1352.Product of the Last K Numbers/README_EN.md index c22336b3e4557..5333ea38575f1 100644 --- a/solution/1300-1399/1352.Product of the Last K Numbers/README_EN.md +++ b/solution/1300-1399/1352.Product of the Last K Numbers/README_EN.md @@ -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
@@ -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]; + } +} +``` + diff --git a/solution/1300-1399/1352.Product of the Last K Numbers/Solution.js b/solution/1300-1399/1352.Product of the Last K Numbers/Solution.js new file mode 100644 index 0000000000000..e1a7c08c5dcfe --- /dev/null +++ b/solution/1300-1399/1352.Product of the Last K Numbers/Solution.js @@ -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]; + } +} diff --git a/solution/1300-1399/1352.Product of the Last K Numbers/Solution.ts b/solution/1300-1399/1352.Product of the Last K Numbers/Solution.ts new file mode 100644 index 0000000000000..a05afac603acf --- /dev/null +++ b/solution/1300-1399/1352.Product of the Last K Numbers/Solution.ts @@ -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]; + } +}