|
1 |
| -# [03.01. Three in One](https://leetcode-cn.com/problems/three-in-one-lcci) |
2 |
| - |
3 |
| -## Description |
4 |
| -<p>Describe how you could use a single array to implement three stacks.</p> |
5 |
| -
|
6 |
| -<p>Yout 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> |
7 |
| -
|
8 |
| -<p>The constructor requires a <code>stackSize</code> parameter, which represents the size of each stack.</p> |
9 |
| -
|
10 |
| -<p><strong>Example1:</strong></p> |
11 |
| -
|
12 |
| -<pre> |
13 |
| -<strong> Input</strong>: |
14 |
| -["TripleInOne", "push", "push", "pop", "pop", "pop", "isEmpty"] |
15 |
| -[[1], [0, 1], [0, 2], [0], [0], [0], [0]] |
16 |
| -<strong> Output</strong>: |
17 |
| -[null, null, null, 1, -1, -1, true] |
18 |
| -<b>Explanation</b>: When the stack is empty, `pop, peek` return -1. When the stack is full, `push` does nothing. |
19 |
| -</pre> |
20 |
| -
|
21 |
| -<p><strong>Example2:</strong></p> |
22 |
| -
|
23 |
| -<pre> |
24 |
| -<strong> Input</strong>: |
25 |
| -["TripleInOne", "push", "push", "push", "pop", "pop", "pop", "peek"] |
26 |
| -[[2], [0, 1], [0, 2], [0, 3], [0], [0], [0], [0]] |
27 |
| -<strong> Output</strong>: |
28 |
| -[null, null, null, null, 2, 1, -1, -1] |
29 |
| -</pre> |
30 |
| - |
31 |
| - |
32 |
| - |
33 |
| -## Solutions |
34 |
| - |
35 |
| - |
36 |
| -### Python3 |
37 |
| - |
38 |
| -```python |
39 |
| - |
40 |
| -``` |
41 |
| - |
42 |
| -### Java |
43 |
| - |
44 |
| -```java |
45 |
| - |
46 |
| -``` |
47 |
| - |
48 |
| -### ... |
49 |
| -``` |
50 |
| - |
51 |
| -``` |
| 1 | +# [03.01. Three in One](https://leetcode-cn.com/problems/three-in-one-lcci) |
| 2 | + |
| 3 | +## Description |
| 4 | +<p>Describe how you could use a single array to implement three stacks.</p> |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | +<p>Yout 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> |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | +<p>The constructor requires a <code>stackSize</code> parameter, which represents the size of each stack.</p> |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | +<p><strong>Example1:</strong></p> |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | +<pre> |
| 21 | + |
| 22 | +<strong> Input</strong>: |
| 23 | + |
| 24 | +["TripleInOne", "push", "push", "pop", "pop", "pop", "isEmpty"] |
| 25 | + |
| 26 | +[[1], [0, 1], [0, 2], [0], [0], [0], [0]] |
| 27 | + |
| 28 | +<strong> Output</strong>: |
| 29 | + |
| 30 | +[null, null, null, 1, -1, -1, true] |
| 31 | + |
| 32 | +<b>Explanation</b>: When the stack is empty, `pop, peek` return -1. When the stack is full, `push` does nothing. |
| 33 | + |
| 34 | +</pre> |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | +<p><strong>Example2:</strong></p> |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | +<pre> |
| 43 | + |
| 44 | +<strong> Input</strong>: |
| 45 | + |
| 46 | +["TripleInOne", "push", "push", "push", "pop", "pop", "pop", "peek"] |
| 47 | + |
| 48 | +[[2], [0, 1], [0, 2], [0, 3], [0], [0], [0], [0]] |
| 49 | + |
| 50 | +<strong> Output</strong>: |
| 51 | + |
| 52 | +[null, null, null, null, 2, 1, -1, -1] |
| 53 | + |
| 54 | +</pre> |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | +## Solutions |
| 60 | + |
| 61 | + |
| 62 | +### Python3 |
| 63 | + |
| 64 | +```python |
| 65 | +class TripleInOne: |
| 66 | + |
| 67 | + def __init__(self, stackSize: int): |
| 68 | + self._capacity = stackSize |
| 69 | + self._s = [[], [], []] |
| 70 | + |
| 71 | + def push(self, stackNum: int, value: int) -> None: |
| 72 | + if len(self._s[stackNum]) < self._capacity: |
| 73 | + self._s[stackNum].append(value) |
| 74 | + |
| 75 | + def pop(self, stackNum: int) -> int: |
| 76 | + return -1 if self.isEmpty(stackNum) else self._s[stackNum].pop() |
| 77 | + |
| 78 | + def peek(self, stackNum: int) -> int: |
| 79 | + return -1 if self.isEmpty(stackNum) else self._s[stackNum][-1] |
| 80 | + |
| 81 | + def isEmpty(self, stackNum: int) -> bool: |
| 82 | + return len(self._s[stackNum]) == 0 |
| 83 | + |
| 84 | + |
| 85 | +# Your TripleInOne object will be instantiated and called as such: |
| 86 | +# obj = TripleInOne(stackSize) |
| 87 | +# obj.push(stackNum,value) |
| 88 | +# param_2 = obj.pop(stackNum) |
| 89 | +# param_3 = obj.peek(stackNum) |
| 90 | +# param_4 = obj.isEmpty(stackNum) |
| 91 | +``` |
| 92 | + |
| 93 | +### Java |
| 94 | + |
| 95 | +```java |
| 96 | +class TripleInOne { |
| 97 | + private int[] s; |
| 98 | + private int capacity; |
| 99 | + |
| 100 | + public TripleInOne(int stackSize) { |
| 101 | + s = new int[stackSize * 3 + 3]; |
| 102 | + capacity = stackSize; |
| 103 | + } |
| 104 | + |
| 105 | + public void push(int stackNum, int value) { |
| 106 | + if (s[stackNum + 3 * capacity] < capacity) { |
| 107 | + s[s[stackNum + 3 * capacity] * 3 + stackNum] = value; |
| 108 | + ++s[stackNum + 3 * capacity]; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + public int pop(int stackNum) { |
| 113 | + if (isEmpty(stackNum)) { |
| 114 | + return -1; |
| 115 | + } |
| 116 | + --s[stackNum + 3 * capacity]; |
| 117 | + return s[s[stackNum + 3 * capacity] * 3 + stackNum]; |
| 118 | + } |
| 119 | + |
| 120 | + public int peek(int stackNum) { |
| 121 | + return isEmpty(stackNum) ? -1 : s[(s[stackNum + 3 * capacity] - 1) * 3 + stackNum]; |
| 122 | + } |
| 123 | + |
| 124 | + public boolean isEmpty(int stackNum) { |
| 125 | + return s[stackNum + 3 * capacity] == 0; |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +/** |
| 130 | + * Your TripleInOne object will be instantiated and called as such: |
| 131 | + * TripleInOne obj = new TripleInOne(stackSize); |
| 132 | + * obj.push(stackNum,value); |
| 133 | + * int param_2 = obj.pop(stackNum); |
| 134 | + * int param_3 = obj.peek(stackNum); |
| 135 | + * boolean param_4 = obj.isEmpty(stackNum); |
| 136 | + */ |
| 137 | +``` |
| 138 | + |
| 139 | +### ... |
| 140 | +``` |
| 141 | +
|
| 142 | +``` |
0 commit comments