mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-12-21 03:13:45 +08:00
update
This commit is contained in:
@@ -1,26 +1,22 @@
|
||||
<p>请你实现一个「数字乘积类」<code>ProductOfNumbers</code>,要求支持下述两种方法:</p>
|
||||
<p>设计一个算法,该算法接受一个整数流并检索该流中最后 <code>k</code> 个整数的乘积。</p>
|
||||
|
||||
<p>1.<code> add(int num)</code></p>
|
||||
<p>实现 <code>ProductOfNumbers</code> 类:</p>
|
||||
|
||||
<ul>
|
||||
<li>将数字 <code>num</code> 添加到当前数字列表的最后面。</li>
|
||||
<li><code>ProductOfNumbers()</code> 用一个空的流初始化对象。</li>
|
||||
<li><code>void add(int num)</code> 将数字 <code>num</code> 添加到当前数字列表的最后面。</li>
|
||||
<li><code>int getProduct(int k)</code> 返回当前数字列表中,最后 <code>k</code> 个数字的乘积。你可以假设当前列表中始终 <strong>至少</strong> 包含 <code>k</code> 个数字。</li>
|
||||
</ul>
|
||||
|
||||
<p>2.<code> getProduct(int k)</code></p>
|
||||
|
||||
<ul>
|
||||
<li>返回当前数字列表中,最后 <code>k</code> 个数字的乘积。</li>
|
||||
<li>你可以假设当前列表中始终 <strong>至少</strong> 包含 <code>k</code> 个数字。</li>
|
||||
</ul>
|
||||
|
||||
<p>题目数据保证:任何时候,任一连续数字序列的乘积都在 32-bit 整数范围内,不会溢出。</p>
|
||||
<p>题目数据保证:任何时候,任一连续数字序列的乘积都在 32 位整数范围内,不会溢出。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>
|
||||
["ProductOfNumbers","add","add","add","add","add","getProduct","getProduct","getProduct","add","getProduct"]
|
||||
<pre>
|
||||
<strong>输入:</strong>
|
||||
["ProductOfNumbers","add","add","add","add","add","getProduct","getProduct","getProduct","add","getProduct"]
|
||||
[[],[3],[0],[2],[5],[4],[2],[3],[4],[8],[2]]
|
||||
|
||||
<strong>输出:</strong>
|
||||
@@ -45,7 +41,12 @@ productOfNumbers.getProduct(2); // 返回 32 。最后 2 个数字的乘积是 4
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>add</code> 和 <code>getProduct</code> 两种操作加起来总共不会超过 <code>40000</code> 次。</li>
|
||||
<li><code>0 <= num <= 100</code></li>
|
||||
<li><code>1 <= k <= 40000</code></li>
|
||||
<li><code>1 <= k <= 4 * 10<sup>4</sup></code></li>
|
||||
<li><code>add</code> 和 <code>getProduct</code> 最多被调用 <code>4 * 10<sup>4</sup></code> 次。</li>
|
||||
<li>在任何时间点流的乘积都在 32 位整数范围内。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>进阶:</strong>您能否 <strong>同时</strong> 将 <code>GetProduct</code> 和 <code>Add</code> 的实现改为 <code>O(1)</code> 时间复杂度,而不是 <code>O(k)</code> 时间复杂度?</p>
|
||||
|
||||
Reference in New Issue
Block a user