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)/最小栈 [bao-han-minhan-shu-de-zhan-lcof].html
2023-12-09 18:53:53 +08:00

54 lines
1.7 KiB
HTML
Raw 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>请你设计一个 <strong>最小栈</strong> 。它提供 <code>push</code> <code>pop</code> <code>top</code> 操作,并能在常数时间内检索到最小元素的栈。</p>
<p>&nbsp;</p>
<p>实现 <code>MinStack</code> 类:</p>
<ul>
<li><code>MinStack()</code> 初始化堆栈对象。</li>
<li><code>void push(int val)</code> 将元素val推入堆栈。</li>
<li><code>void pop()</code> 删除堆栈顶部的元素。</li>
<li><code>int top()</code> 获取堆栈顶部的元素。</li>
<li><code>int getMin()</code> 获取堆栈中的最小元素。</li>
</ul>
<p>&nbsp;</p>
<p><strong>示例 1:</strong></p>
<pre>
<strong>输入:</strong>
["MinStack","push","push","push","getMin","pop","top","getMin"]
[[],[-2],[2],[-3],[],[],[],[]]
<strong>输出:</strong>
[null,null,null,null,-3,null,2,-2]
<strong>解释:</strong>
MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(2);
minStack.push(-3);
minStack.getMin(); &nbsp; --&gt; 返回 -3.
minStack.pop();
minStack.top(); &nbsp; &nbsp; &nbsp;--&gt; 返回 2.
minStack.getMin(); &nbsp; --&gt; 返回 -2.
</pre>
<p>&nbsp;</p>
<p><strong>&nbsp;<br />
提示:</strong></p>
<ul>
<li><code>-2<sup>31</sup> &lt;= val &lt;= 2<sup>31</sup> - 1</code></li>
<li><code>pop</code><code>top</code><code>getMin</code> 操作总是在 <strong>非空栈</strong> 上调用</li>
<li><code>push</code><code>pop</code><code>top</code><code>getMin</code> 最多被调用 <code>3 * 10<sup>4</sup></code></li>
</ul>
<p>&nbsp;</p>
<p>注意:本题与主站 155 题相同:<a href="https://leetcode-cn.com/problems/min-stack/">https://leetcode-cn.com/problems/min-stack/</a></p>
<p>&nbsp;</p>