mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 18:48:13 +08:00
25 lines
1.5 KiB
HTML
25 lines
1.5 KiB
HTML
<p>Imagine a (literal) stack of plates. If the stack gets too high, it might topple. Therefore, in real life, we would likely start a new stack when the previous stack exceeds some threshold. Implement a data structure <code>SetOfStacks</code> that mimics this. <code>SetOfStacks</code> should be composed of several stacks and should create a new stack once the previous one exceeds capacity. <code>SetOfStacks.push()</code> and <code>SetOfStacks.pop()</code> should behave identically to a single stack (that is, <code>pop()</code> should return the same values as it would if there were just a single stack). Follow Up: Implement a function <code>popAt(int index)</code> which performs a pop operation on a specific sub-stack.</p>
|
|
|
|
|
|
|
|
<p>You should delete the sub-stack when it becomes empty. <code>pop</code>, <code>popAt</code> should return -1 when there's no element to pop.</p>
|
|
|
|
|
|
|
|
<p><strong>Example1:</strong></p>
|
|
|
|
|
|
|
|
<pre>
|
|
|
|
<strong> Input</strong>:
|
|
|
|
["StackOfPlates", "push", "push", "popAt", "pop", "pop"]
|
|
|
|
[[1], [1], [2], [1], [], []]
|
|
|
|
<strong> Output</strong>:
|
|
|
|
[null, null, null, 2, 1, -1]
|
|
|
|
<strong> Explanation</strong>:
|
|
|
|
</pre>
|
|
|
|
|
|
|
|
<p><strong>Example2:</strong></p>
|
|
|
|
|
|
|
|
<pre>
|
|
|
|
<strong> Input</strong>:
|
|
|
|
["StackOfPlates", "push", "push", "push", "popAt", "popAt", "popAt"]
|
|
|
|
[[2], [1], [2], [3], [0], [0], [0]]
|
|
|
|
<strong> Output</strong>:
|
|
|
|
[null, null, null, null, 2, 1, 3]
|
|
|
|
</pre>
|
|
|