-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create Code Testcase Test Result Test Result 1352. Product of the La… (…
- Loading branch information
Showing
1 changed file
with
27 additions
and
0 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
Code Testcase Test Result Test Result 1352. Product of the Last K Numbers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
class ProductOfNumbers { | ||
public: | ||
vector<int> nums; | ||
int n; | ||
ProductOfNumbers() { | ||
nums.clear(); | ||
n = 0; | ||
} | ||
|
||
void add(int num) { nums.push_back(num); } | ||
|
||
int getProduct(int k) { | ||
int n = nums.size(); | ||
int prod = 1; | ||
for (int i = n - k; i < n; i++) { | ||
prod *= nums[i]; | ||
} | ||
return prod; | ||
} | ||
}; | ||
|
||
/** | ||
* Your ProductOfNumbers object will be instantiated and called as such: | ||
* ProductOfNumbers* obj = new ProductOfNumbers(); | ||
* obj->add(num); | ||
* int param_2 = obj->getProduct(k); | ||
*/ |