We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 58878fd commit 6a4ed92Copy full SHA for 6a4ed92
Medium/Product of the Last K Numbers.java
@@ -1,21 +1,24 @@
1
class ProductOfNumbers {
2
- List<Integer> list;
+
3
+ private List<Integer> list;
4
5
public ProductOfNumbers() {
6
+ this.list = new ArrayList<>();
7
add(0);
8
}
9
10
public void add(int num) {
11
if (num > 0) {
12
list.add(list.get(list.size() - 1) * num);
- }
- else {
13
+ } else {
14
list = new ArrayList<>();
15
list.add(1);
16
17
18
19
public int getProduct(int k) {
- return k < list.size() ? list.get(list.size() - 1) / list.get(list.size() - k - 1) : 0;
20
+ int n = list.size();
21
+ return k < n ? list.get(n - 1) / list.get(n - k - 1) : 0;
22
23
24
0 commit comments