Skip to content

Commit a625f1a

Browse files
committed
feat: add solutions to lc problem: No.1352. Product of the Last K Numbers
1 parent 95e4036 commit a625f1a

File tree

4 files changed

+155
-4
lines changed

4 files changed

+155
-4
lines changed

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

+54-2
Original file line numberDiff line numberDiff line change
@@ -58,27 +58,79 @@ productOfNumbers.getProduct(2); // 返回 32 。最后 2 个数字的乘积是 4
5858
<li><code>1 &lt;= k &lt;= 40000</code></li>
5959
</ul>
6060

61-
6261
## 解法
6362

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

65+
“前缀积”实现。
66+
67+
若遇到 0,则清空前缀积列表。
68+
6669
<!-- tabs:start -->
6770

6871
### **Python3**
6972

7073
<!-- 这里可写当前语言的特殊实现逻辑 -->
7174

7275
```python
76+
class ProductOfNumbers:
77+
78+
def __init__(self):
79+
self.pre_product = []
7380

81+
def add(self, num: int) -> None:
82+
if num == 0:
83+
self.pre_product = []
84+
return
85+
if not self.pre_product:
86+
self.pre_product.append(1)
87+
self.pre_product.append(num * self.pre_product[-1])
88+
89+
def getProduct(self, k: int) -> int:
90+
n = len(self.pre_product)
91+
return 0 if n <= k else self.pre_product[n - 1] // self.pre_product[n - k - 1]
92+
93+
94+
# Your ProductOfNumbers object will be instantiated and called as such:
95+
# obj = ProductOfNumbers()
96+
# obj.add(num)
97+
# param_2 = obj.getProduct(k)
7498
```
7599

76100
### **Java**
77101

78102
<!-- 这里可写当前语言的特殊实现逻辑 -->
79103

80104
```java
81-
105+
class ProductOfNumbers {
106+
private List<Integer> preProduct;
107+
108+
public ProductOfNumbers() {
109+
preProduct = new ArrayList<>();
110+
}
111+
112+
public void add(int num) {
113+
if (num == 0) {
114+
preProduct.clear();
115+
return;
116+
}
117+
if (preProduct.isEmpty()) {
118+
preProduct.add(1);
119+
}
120+
preProduct.add(num * preProduct.get(preProduct.size() - 1));
121+
}
122+
123+
public int getProduct(int k) {
124+
return preProduct.size() <= k ? 0 : preProduct.get(preProduct.size() - 1) / preProduct.get(preProduct.size() - 1 - k);
125+
}
126+
}
127+
128+
/**
129+
* Your ProductOfNumbers object will be instantiated and called as such:
130+
* ProductOfNumbers obj = new ProductOfNumbers();
131+
* obj.add(num);
132+
* int param_2 = obj.getProduct(k);
133+
*/
82134
```
83135

84136
### **...**

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

+50-2
Original file line numberDiff line numberDiff line change
@@ -55,21 +55,69 @@ productOfNumbers.getProduct(2); // return 32. The product of the last 2 numbers
5555
<li><code>1 &lt;= k &lt;= 40000</code></li>
5656
</ul>
5757

58-
5958
## Solutions
6059

6160
<!-- tabs:start -->
6261

6362
### **Python3**
6463

6564
```python
65+
class ProductOfNumbers:
66+
67+
def __init__(self):
68+
self.pre_product = []
69+
70+
def add(self, num: int) -> None:
71+
if num == 0:
72+
self.pre_product = []
73+
return
74+
if not self.pre_product:
75+
self.pre_product.append(1)
76+
self.pre_product.append(num * self.pre_product[-1])
6677

78+
def getProduct(self, k: int) -> int:
79+
n = len(self.pre_product)
80+
return 0 if n <= k else self.pre_product[n - 1] // self.pre_product[n - k - 1]
81+
82+
83+
# Your ProductOfNumbers object will be instantiated and called as such:
84+
# obj = ProductOfNumbers()
85+
# obj.add(num)
86+
# param_2 = obj.getProduct(k)
6787
```
6888

6989
### **Java**
7090

7191
```java
72-
92+
class ProductOfNumbers {
93+
private List<Integer> preProduct;
94+
95+
public ProductOfNumbers() {
96+
preProduct = new ArrayList<>();
97+
}
98+
99+
public void add(int num) {
100+
if (num == 0) {
101+
preProduct.clear();
102+
return;
103+
}
104+
if (preProduct.isEmpty()) {
105+
preProduct.add(1);
106+
}
107+
preProduct.add(num * preProduct.get(preProduct.size() - 1));
108+
}
109+
110+
public int getProduct(int k) {
111+
return preProduct.size() <= k ? 0 : preProduct.get(preProduct.size() - 1) / preProduct.get(preProduct.size() - 1 - k);
112+
}
113+
}
114+
115+
/**
116+
* Your ProductOfNumbers object will be instantiated and called as such:
117+
* ProductOfNumbers obj = new ProductOfNumbers();
118+
* obj.add(num);
119+
* int param_2 = obj.getProduct(k);
120+
*/
73121
```
74122

75123
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class ProductOfNumbers {
2+
private List<Integer> preProduct;
3+
4+
public ProductOfNumbers() {
5+
preProduct = new ArrayList<>();
6+
}
7+
8+
public void add(int num) {
9+
if (num == 0) {
10+
preProduct.clear();
11+
return;
12+
}
13+
if (preProduct.isEmpty()) {
14+
preProduct.add(1);
15+
}
16+
preProduct.add(num * preProduct.get(preProduct.size() - 1));
17+
}
18+
19+
public int getProduct(int k) {
20+
return preProduct.size() <= k ? 0 : preProduct.get(preProduct.size() - 1) / preProduct.get(preProduct.size() - 1 - k);
21+
}
22+
}
23+
24+
/**
25+
* Your ProductOfNumbers object will be instantiated and called as such:
26+
* ProductOfNumbers obj = new ProductOfNumbers();
27+
* obj.add(num);
28+
* int param_2 = obj.getProduct(k);
29+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class ProductOfNumbers:
2+
3+
def __init__(self):
4+
self.pre_product = []
5+
6+
def add(self, num: int) -> None:
7+
if num == 0:
8+
self.pre_product = []
9+
return
10+
if not self.pre_product:
11+
self.pre_product.append(1)
12+
self.pre_product.append(num * self.pre_product[-1])
13+
14+
def getProduct(self, k: int) -> int:
15+
n = len(self.pre_product)
16+
return 0 if n <= k else self.pre_product[n - 1] // self.pre_product[n - k - 1]
17+
18+
19+
# Your ProductOfNumbers object will be instantiated and called as such:
20+
# obj = ProductOfNumbers()
21+
# obj.add(num)
22+
# param_2 = obj.getProduct(k)

0 commit comments

Comments
 (0)