mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 18:48:13 +08:00
27 lines
1.4 KiB
HTML
27 lines
1.4 KiB
HTML
<p>Describe how you could use a single array to implement three stacks.</p>
|
|
|
|
|
|
|
|
<p>You should implement <code>push(stackNum, value)</code>、<code>pop(stackNum)</code>、<code>isEmpty(stackNum)</code>、<code>peek(stackNum)</code> methods. <code>stackNum<font face="sans-serif, Arial, Verdana, Trebuchet MS"> </font></code><font face="sans-serif, Arial, Verdana, Trebuchet MS">is the index of the stack. </font><code>value</code> is the value that pushed to the stack.</p>
|
|
|
|
|
|
|
|
<p>The constructor requires a <code>stackSize</code> parameter, which represents the size of each stack.</p>
|
|
|
|
|
|
|
|
<p><strong>Example1:</strong></p>
|
|
|
|
|
|
|
|
<pre>
|
|
|
|
<strong> Input</strong>:
|
|
|
|
["TripleInOne", "push", "push", "pop", "pop", "pop", "isEmpty"]
|
|
|
|
[[1], [0, 1], [0, 2], [0], [0], [0], [0]]
|
|
|
|
<strong> Output</strong>:
|
|
|
|
[null, null, null, 1, -1, -1, true]
|
|
|
|
<b>Explanation</b>: When the stack is empty, `pop, peek` return -1. When the stack is full, `push` does nothing.
|
|
|
|
</pre>
|
|
|
|
|
|
|
|
<p><strong>Example2:</strong></p>
|
|
|
|
|
|
|
|
<pre>
|
|
|
|
<strong> Input</strong>:
|
|
|
|
["TripleInOne", "push", "push", "push", "pop", "pop", "pop", "peek"]
|
|
|
|
[[2], [0, 1], [0, 2], [0, 3], [0], [0], [0], [0]]
|
|
|
|
<strong> Output</strong>:
|
|
|
|
[null, null, null, null, 2, 1, -1, -1]
|
|
|
|
</pre>
|
|
|