1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-10-24 14:28:56 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
This commit is contained in:
2022-05-02 23:44:12 +08:00
parent 7ea03594b3
commit 2a71c78585
4790 changed files with 11696 additions and 10944 deletions

View File

@@ -12,7 +12,7 @@
"translatedContent": "<p>请你设计一个支持下述操作的栈。</p>\n\n<p>实现自定义栈类 <code>CustomStack</code> </p>\n\n<ul>\n\t<li><code>CustomStack(int maxSize)</code>:用 <code>maxSize</code> 初始化对象,<code>maxSize</code> 是栈中最多能容纳的元素数量,栈在增长到 <code>maxSize</code> 之后则不支持 <code>push</code> 操作。</li>\n\t<li><code>void push(int x)</code>:如果栈还未增长到 <code>maxSize</code> ,就将 <code>x</code> 添加到栈顶。</li>\n\t<li><code>int pop()</code>:弹出栈顶元素,并返回栈顶的值,或栈为空时返回 <strong>-1</strong> 。</li>\n\t<li><code>void inc(int k, int val)</code>:栈底的 <code>k</code> 个元素的值都增加 <code>val</code> 。如果栈中元素总数小于 <code>k</code> ,则栈中的所有元素都增加 <code>val</code> 。</li>\n</ul>\n\n<p>&nbsp;</p>\n\n<p><strong>示例:</strong></p>\n\n<pre><strong>输入:</strong>\n[&quot;CustomStack&quot;,&quot;push&quot;,&quot;push&quot;,&quot;pop&quot;,&quot;push&quot;,&quot;push&quot;,&quot;push&quot;,&quot;increment&quot;,&quot;increment&quot;,&quot;pop&quot;,&quot;pop&quot;,&quot;pop&quot;,&quot;pop&quot;]\n[[3],[1],[2],[],[2],[3],[4],[5,100],[2,100],[],[],[],[]]\n<strong>输出:</strong>\n[null,null,null,2,null,null,null,null,null,103,202,201,-1]\n<strong>解释:</strong>\nCustomStack customStack = new CustomStack(3); // 栈是空的 []\ncustomStack.push(1); // 栈变为 [1]\ncustomStack.push(2); // 栈变为 [1, 2]\ncustomStack.pop(); // 返回 2 --&gt; 返回栈顶值 2栈变为 [1]\ncustomStack.push(2); // 栈变为 [1, 2]\ncustomStack.push(3); // 栈变为 [1, 2, 3]\ncustomStack.push(4); // 栈仍然是 [1, 2, 3],不能添加其他元素使栈大小变为 4\ncustomStack.increment(5, 100); // 栈变为 [101, 102, 103]\ncustomStack.increment(2, 100); // 栈变为 [201, 202, 103]\ncustomStack.pop(); // 返回 103 --&gt; 返回栈顶值 103栈变为 [201, 202]\ncustomStack.pop(); // 返回 202 --&gt; 返回栈顶值 202栈变为 [201]\ncustomStack.pop(); // 返回 201 --&gt; 返回栈顶值 201栈变为 []\ncustomStack.pop(); // 返回 -1 --&gt; 栈为空,返回 -1\n</pre>\n\n<p>&nbsp;</p>\n\n<p><strong>提示:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= maxSize &lt;= 1000</code></li>\n\t<li><code>1 &lt;= x &lt;= 1000</code></li>\n\t<li><code>1 &lt;= k &lt;= 1000</code></li>\n\t<li><code>0 &lt;= val &lt;= 100</code></li>\n\t<li>每种方法 <code>increment</code><code>push</code> 以及 <code>pop</code> 分别最多调用 <code>1000</code> 次</li>\n</ul>\n",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 70,
"likes": 72,
"dislikes": 0,
"isLiked": null,
"similarQuestions": "[]",
@@ -149,7 +149,7 @@
"__typename": "CodeSnippetNode"
}
],
"stats": "{\"totalAccepted\": \"13.8K\", \"totalSubmission\": \"19K\", \"totalAcceptedRaw\": 13800, \"totalSubmissionRaw\": 18951, \"acRate\": \"72.8%\"}",
"stats": "{\"totalAccepted\": \"14.4K\", \"totalSubmission\": \"19.7K\", \"totalAcceptedRaw\": 14369, \"totalSubmissionRaw\": 19710, \"acRate\": \"72.9%\"}",
"hints": [
"Use an array to represent the stack. Push will add new integer to the array. Pop removes the last element in the array and increment will add val to the first k elements of the array.",
"This solution run in O(1) per push and pop and O(k) per increment."