1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-10 18:48:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/用队列实现栈 [implement-stack-using-queues].html
2022-03-29 12:43:11 +08:00

54 lines
1.8 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<p>请你仅使用两个队列实现一个后入先出LIFO的栈并支持普通栈的全部四种操作<code>push</code><code>top</code><code>pop</code><code>empty</code>)。</p>
<p>实现 <code>MyStack</code> 类:</p>
<ul>
<li><code>void push(int x)</code> 将元素 x 压入栈顶。</li>
<li><code>int pop()</code> 移除并返回栈顶元素。</li>
<li><code>int top()</code> 返回栈顶元素。</li>
<li><code>boolean empty()</code> 如果栈是空的,返回 <code>true</code> ;否则,返回 <code>false</code></li>
</ul>
<p>&nbsp;</p>
<p><strong>注意:</strong></p>
<ul>
<li>你只能使用队列的基本操作 —— 也就是&nbsp;<code>push to back</code><code>peek/pop from front</code><code>size</code>&nbsp;<code>is empty</code>&nbsp;这些操作。</li>
<li>你所使用的语言也许不支持队列。&nbsp;你可以使用 list (列表)或者 deque双端队列来模拟一个队列&nbsp;, 只要是标准的队列操作即可。</li>
</ul>
<p>&nbsp;</p>
<p><strong>示例:</strong></p>
<pre>
<strong>输入:</strong>
["MyStack", "push", "push", "top", "pop", "empty"]
[[], [1], [2], [], [], []]
<strong>输出:</strong>
[null, null, null, 2, 2, false]
<strong>解释:</strong>
MyStack myStack = new MyStack();
myStack.push(1);
myStack.push(2);
myStack.top(); // 返回 2
myStack.pop(); // 返回 2
myStack.empty(); // 返回 False
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= x &lt;= 9</code></li>
<li>最多调用<code>100</code><code>push</code><code>pop</code><code>top</code><code>empty</code></li>
<li>每次调用 <code>pop</code><code>top</code> 都保证栈不为空</li>
</ul>
<p>&nbsp;</p>
<p><strong>进阶:</strong>你能否仅用一个队列来实现栈。</p>