diff --git a/images/contributors.svg b/images/contributors.svg index 57c695148d8aa..b5bdd1e3122cb 100644 --- a/images/contributors.svg +++ b/images/contributors.svg @@ -63,11 +63,11 @@ - - - - + + + + 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 2d49e546ade54..81d038d2fbbfc 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 @@ -58,11 +58,14 @@ productOfNumbers.getProduct(2); // 返回 32 。最后 2 个数字的乘积是 4
  • 1 <= k <= 40000
  • - ## 解法 +“前缀积”实现。 + +若遇到 0,则清空前缀积列表。 + ### **Python3** @@ -70,7 +73,28 @@ productOfNumbers.getProduct(2); // 返回 32 。最后 2 个数字的乘积是 4 ```python +class ProductOfNumbers: + + def __init__(self): + self.pre_product = [] + def add(self, num: int) -> None: + if num == 0: + self.pre_product = [] + return + if not self.pre_product: + self.pre_product.append(1) + self.pre_product.append(num * self.pre_product[-1]) + + def getProduct(self, k: int) -> int: + n = len(self.pre_product) + return 0 if n <= k else self.pre_product[n - 1] // self.pre_product[n - k - 1] + + +# Your ProductOfNumbers object will be instantiated and called as such: +# obj = ProductOfNumbers() +# obj.add(num) +# param_2 = obj.getProduct(k) ``` ### **Java** @@ -78,7 +102,35 @@ productOfNumbers.getProduct(2); // 返回 32 。最后 2 个数字的乘积是 4 ```java - +class ProductOfNumbers { + private List preProduct; + + public ProductOfNumbers() { + preProduct = new ArrayList<>(); + } + + public void add(int num) { + if (num == 0) { + preProduct.clear(); + return; + } + if (preProduct.isEmpty()) { + preProduct.add(1); + } + preProduct.add(num * preProduct.get(preProduct.size() - 1)); + } + + public int getProduct(int k) { + return preProduct.size() <= k ? 0 : preProduct.get(preProduct.size() - 1) / preProduct.get(preProduct.size() - 1 - k); + } +} + +/** + * Your ProductOfNumbers object will be instantiated and called as such: + * ProductOfNumbers obj = new ProductOfNumbers(); + * obj.add(num); + * int param_2 = obj.getProduct(k); + */ ``` ### **...** 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 5320f1b2c9816..c7d7fc6db6185 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 @@ -55,7 +55,6 @@ productOfNumbers.getProduct(2); // return 32. The product of the last 2 numbers
  • 1 <= k <= 40000
  • - ## Solutions @@ -63,13 +62,62 @@ productOfNumbers.getProduct(2); // return 32. The product of the last 2 numbers ### **Python3** ```python +class ProductOfNumbers: + + def __init__(self): + self.pre_product = [] + + def add(self, num: int) -> None: + if num == 0: + self.pre_product = [] + return + if not self.pre_product: + self.pre_product.append(1) + self.pre_product.append(num * self.pre_product[-1]) + def getProduct(self, k: int) -> int: + n = len(self.pre_product) + return 0 if n <= k else self.pre_product[n - 1] // self.pre_product[n - k - 1] + + +# Your ProductOfNumbers object will be instantiated and called as such: +# obj = ProductOfNumbers() +# obj.add(num) +# param_2 = obj.getProduct(k) ``` ### **Java** ```java - +class ProductOfNumbers { + private List preProduct; + + public ProductOfNumbers() { + preProduct = new ArrayList<>(); + } + + public void add(int num) { + if (num == 0) { + preProduct.clear(); + return; + } + if (preProduct.isEmpty()) { + preProduct.add(1); + } + preProduct.add(num * preProduct.get(preProduct.size() - 1)); + } + + public int getProduct(int k) { + return preProduct.size() <= k ? 0 : preProduct.get(preProduct.size() - 1) / preProduct.get(preProduct.size() - 1 - k); + } +} + +/** + * Your ProductOfNumbers object will be instantiated and called as such: + * ProductOfNumbers obj = new ProductOfNumbers(); + * obj.add(num); + * int param_2 = obj.getProduct(k); + */ ``` ### **...** diff --git a/solution/1300-1399/1352.Product of the Last K Numbers/Solution.java b/solution/1300-1399/1352.Product of the Last K Numbers/Solution.java new file mode 100644 index 0000000000000..1414b371dbd56 --- /dev/null +++ b/solution/1300-1399/1352.Product of the Last K Numbers/Solution.java @@ -0,0 +1,29 @@ +class ProductOfNumbers { + private List preProduct; + + public ProductOfNumbers() { + preProduct = new ArrayList<>(); + } + + public void add(int num) { + if (num == 0) { + preProduct.clear(); + return; + } + if (preProduct.isEmpty()) { + preProduct.add(1); + } + preProduct.add(num * preProduct.get(preProduct.size() - 1)); + } + + public int getProduct(int k) { + return preProduct.size() <= k ? 0 : preProduct.get(preProduct.size() - 1) / preProduct.get(preProduct.size() - 1 - k); + } +} + +/** + * Your ProductOfNumbers object will be instantiated and called as such: + * ProductOfNumbers obj = new ProductOfNumbers(); + * obj.add(num); + * int param_2 = obj.getProduct(k); + */ \ No newline at end of file diff --git a/solution/1300-1399/1352.Product of the Last K Numbers/Solution.py b/solution/1300-1399/1352.Product of the Last K Numbers/Solution.py new file mode 100644 index 0000000000000..1ca1b2ee0d598 --- /dev/null +++ b/solution/1300-1399/1352.Product of the Last K Numbers/Solution.py @@ -0,0 +1,22 @@ +class ProductOfNumbers: + + def __init__(self): + self.pre_product = [] + + def add(self, num: int) -> None: + if num == 0: + self.pre_product = [] + return + if not self.pre_product: + self.pre_product.append(1) + self.pre_product.append(num * self.pre_product[-1]) + + def getProduct(self, k: int) -> int: + n = len(self.pre_product) + return 0 if n <= k else self.pre_product[n - 1] // self.pre_product[n - k - 1] + + +# Your ProductOfNumbers object will be instantiated and called as such: +# obj = ProductOfNumbers() +# obj.add(num) +# param_2 = obj.getProduct(k)