|
1 | 1 | # [03.03. Stack of Plates](https://leetcode.cn/problems/stack-of-plates-lcci)
|
| 2 | + |
2 | 3 | [中文文档](/lcci/03.03.Stack%20of%20Plates/README.md)
|
| 4 | + |
3 | 5 | ## Description
|
| 6 | + |
4 | 7 | <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>
|
5 | 8 | <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>
|
6 | 9 | <p><strong>Example1:</strong></p>
|
7 | 10 | <pre>
|
8 | 11 |
|
9 |
| -<strong> Input</strong>: |
| 12 | +<strong> Input</strong>: |
10 | 13 |
|
11 | 14 | ["StackOfPlates", "push", "push", "popAt", "pop", "pop"]
|
12 | 15 |
|
13 | 16 | [[1], [1], [2], [1], [], []]
|
14 | 17 |
|
15 |
| -<strong> Output</strong>: |
| 18 | +<strong> Output</strong>: |
16 | 19 |
|
17 | 20 | [null, null, null, 2, 1, -1]
|
18 | 21 |
|
19 |
| -<strong> Explanation</strong>: |
| 22 | +<strong> Explanation</strong>: |
20 | 23 |
|
21 | 24 | </pre>
|
22 | 25 | <p><strong>Example2:</strong></p>
|
23 | 26 | <pre>
|
24 | 27 |
|
25 |
| -<strong> Input</strong>: |
| 28 | +<strong> Input</strong>: |
26 | 29 |
|
27 | 30 | ["StackOfPlates", "push", "push", "push", "popAt", "popAt", "popAt"]
|
28 | 31 |
|
29 | 32 | [[2], [1], [2], [3], [0], [0], [0]]
|
30 | 33 |
|
31 |
| -<strong> Output</strong>: |
| 34 | +<strong> Output</strong>: |
32 | 35 |
|
33 | 36 | [null, null, null, null, 2, 1, 3]
|
34 | 37 |
|
35 | 38 | </pre>
|
| 39 | + |
36 | 40 | ## Solutions
|
| 41 | + |
37 | 42 | <!-- tabs:start -->
|
| 43 | + |
38 | 44 | ### **Python3**
|
| 45 | + |
39 | 46 | ```python
|
40 | 47 |
|
41 | 48 | ```
|
| 49 | + |
42 | 50 | ### **Java**
|
| 51 | + |
43 | 52 | ```java
|
44 | 53 |
|
45 | 54 | ```
|
| 55 | + |
46 | 56 | ### **TypeScript**
|
| 57 | + |
47 | 58 | ```ts
|
48 | 59 | class StackOfPlates {
|
49 | 60 | private cap: number;
|
@@ -96,8 +107,11 @@ class StackOfPlates {
|
96 | 107 | * var param_3 = obj.popAt(index)
|
97 | 108 | */
|
98 | 109 | ```
|
| 110 | + |
99 | 111 | ### **...**
|
| 112 | + |
100 | 113 | ```
|
101 | 114 |
|
102 | 115 | ```
|
| 116 | + |
103 | 117 | <!-- tabs:end -->
|
0 commit comments