|
3 | 3 | importjava.util.ArrayList; |
4 | 4 | importjava.util.List; |
5 | 5 |
|
6 | | -/** |
7 | | - * 1352. Product of the Last K Numbers |
8 | | - * |
9 | | - * Implement the class ProductOfNumbers that supports two methods: |
10 | | - * 1. add(int num) |
11 | | - * Adds the number num to the back of the current list of numbers. |
12 | | - * 2. getProduct(int k) |
13 | | - * Returns the product of the last k numbers in the current list. |
14 | | - * You can assume that always the current list has at least k numbers. |
15 | | - * At any time, the product of any contiguous sequence of numbers will fit into a single 32-bit integer without overflowing. |
16 | | - * |
17 | | - * Example: |
18 | | - * Input |
19 | | - * ["ProductOfNumbers","add","add","add","add","add","getProduct","getProduct","getProduct","add","getProduct"] |
20 | | - * [[],[3],[0],[2],[5],[4],[2],[3],[4],[8],[2]] |
21 | | - * Output |
22 | | - * [null,null,null,null,null,null,20,40,0,null,32] |
23 | | - * Explanation |
24 | | - * ProductOfNumbers productOfNumbers = new ProductOfNumbers(); |
25 | | - * productOfNumbers.add(3); // [3] |
26 | | - * productOfNumbers.add(0); // [3,0] |
27 | | - * productOfNumbers.add(2); // [3,0,2] |
28 | | - * productOfNumbers.add(5); // [3,0,2,5] |
29 | | - * productOfNumbers.add(4); // [3,0,2,5,4] |
30 | | - * productOfNumbers.getProduct(2); // return 20. The product of the last 2 numbers is 5 * 4 = 20 |
31 | | - * productOfNumbers.getProduct(3); // return 40. The product of the last 3 numbers is 2 * 5 * 4 = 40 |
32 | | - * productOfNumbers.getProduct(4); // return 0. The product of the last 4 numbers is 0 * 2 * 5 * 4 = 0 |
33 | | - * productOfNumbers.add(8); // [3,0,2,5,4,8] |
34 | | - * productOfNumbers.getProduct(2); // return 32. The product of the last 2 numbers is 4 * 8 = 32 |
35 | | - * |
36 | | - * Constraints: |
37 | | - * There will be at most 40000 operations considering both add and getProduct. |
38 | | - * 0 <= num <= 100 |
39 | | - * 1 <= k <= 40000 |
40 | | - * */ |
41 | 6 | publicclass_1352 { |
42 | 7 | publicstaticclassSolution1 { |
43 | | -/**credit: https://leetcode.com/problems/product-of-the-last-k-numbers/discuss/510260/JavaC%2B%2BPython-Prefix-Product*/ |
| 8 | +/** |
| 9 | + * credit: https://leetcode.com/problems/product-of-the-last-k-numbers/discuss/510260/JavaC%2B%2BPython-Prefix-Product |
| 10 | + */ |
44 | 11 | publicstaticclassProductOfNumbers { |
45 | 12 |
|
46 | 13 | List<Integer>list; |
|