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
8 changes: 4 additions & 4 deletions images/contributors.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
56 changes: 54 additions & 2 deletions solution/1300-1399/1352.Product of the Last K Numbers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,27 +58,79 @@ productOfNumbers.getProduct(2); // 返回 32 。最后 2 个数字的乘积是 4
<li><code>1 &lt;= k &lt;= 40000</code></li>
</ul>


## 解法

<!-- 这里可写通用的实现逻辑 -->

“前缀积”实现。

若遇到 0,则清空前缀积列表。

<!-- tabs:start -->

### **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<Integer> 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);
*/
```

### **...**
Expand Down
52 changes: 50 additions & 2 deletions solution/1300-1399/1352.Product of the Last K Numbers/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,69 @@ productOfNumbers.getProduct(2); // return 32. The product of the last 2 numbers
<li><code>1 &lt;= k &lt;= 40000</code></li>
</ul>


## Solutions

<!-- tabs:start -->

### **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<Integer> 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);
*/
```

### **...**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class ProductOfNumbers {
private List<Integer> 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);
*/
22 changes: 22 additions & 0 deletions solution/1300-1399/1352.Product of the Last K Numbers/Solution.py
Original file line number Diff line number Diff line change
@@ -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)