Skip to content

Commit 3ce6b93

Browse files
committed
feat: add python and java solutions to lcci question
添加《程序员面试金典》题解:面试题 03.01. 三合一
1 parent 4e3866f commit 3ce6b93

File tree

4 files changed

+274
-53
lines changed

4 files changed

+274
-53
lines changed

lcci/03.01.Three in One/README.md

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,85 @@
3030

3131
## 解法
3232
<!-- 这里可写通用的实现逻辑 -->
33-
33+
二维数组解决;也可以使用一维数组,以下标 `0,3,6,..``1,4,7,..``2,5,8,..` 区分,一维数组最后三个元素记录每个栈的元素个数。
3434

3535
### Python3
3636
<!-- 这里可写当前语言的特殊实现逻辑 -->
3737

3838
```python
39+
class TripleInOne:
40+
41+
def __init__(self, stackSize: int):
42+
self._capacity = stackSize
43+
self._s = [[], [], []]
44+
45+
def push(self, stackNum: int, value: int) -> None:
46+
if len(self._s[stackNum]) < self._capacity:
47+
self._s[stackNum].append(value)
48+
49+
def pop(self, stackNum: int) -> int:
50+
return -1 if self.isEmpty(stackNum) else self._s[stackNum].pop()
3951

52+
def peek(self, stackNum: int) -> int:
53+
return -1 if self.isEmpty(stackNum) else self._s[stackNum][-1]
54+
55+
def isEmpty(self, stackNum: int) -> bool:
56+
return len(self._s[stackNum]) == 0
57+
58+
59+
# Your TripleInOne object will be instantiated and called as such:
60+
# obj = TripleInOne(stackSize)
61+
# obj.push(stackNum,value)
62+
# param_2 = obj.pop(stackNum)
63+
# param_3 = obj.peek(stackNum)
64+
# param_4 = obj.isEmpty(stackNum)
4065
```
4166

4267
### Java
4368
<!-- 这里可写当前语言的特殊实现逻辑 -->
4469

4570
```java
46-
71+
class TripleInOne {
72+
private int[] s;
73+
private int capacity;
74+
75+
public TripleInOne(int stackSize) {
76+
s = new int[stackSize * 3 + 3];
77+
capacity = stackSize;
78+
}
79+
80+
public void push(int stackNum, int value) {
81+
if (s[stackNum + 3 * capacity] < capacity) {
82+
s[s[stackNum + 3 * capacity] * 3 + stackNum] = value;
83+
++s[stackNum + 3 * capacity];
84+
}
85+
}
86+
87+
public int pop(int stackNum) {
88+
if (isEmpty(stackNum)) {
89+
return -1;
90+
}
91+
--s[stackNum + 3 * capacity];
92+
return s[s[stackNum + 3 * capacity] * 3 + stackNum];
93+
}
94+
95+
public int peek(int stackNum) {
96+
return isEmpty(stackNum) ? -1 : s[(s[stackNum + 3 * capacity] - 1) * 3 + stackNum];
97+
}
98+
99+
public boolean isEmpty(int stackNum) {
100+
return s[stackNum + 3 * capacity] == 0;
101+
}
102+
}
103+
104+
/**
105+
* Your TripleInOne object will be instantiated and called as such:
106+
* TripleInOne obj = new TripleInOne(stackSize);
107+
* obj.push(stackNum,value);
108+
* int param_2 = obj.pop(stackNum);
109+
* int param_3 = obj.peek(stackNum);
110+
* boolean param_4 = obj.isEmpty(stackNum);
111+
*/
47112
```
48113

49114
### ...

lcci/03.01.Three in One/README_EN.md

Lines changed: 142 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,142 @@
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&nbsp;<code>push(stackNum, value)</code>、<code>pop(stackNum)</code>、<code>isEmpty(stackNum)</code>、<code>peek(stackNum)</code>&nbsp;methods.&nbsp;<code>stackNum<font face="sans-serif, Arial, Verdana, Trebuchet MS">&nbsp;</font></code><font face="sans-serif, Arial, Verdana, Trebuchet MS">is the index of the stack.&nbsp;</font><code>value</code>&nbsp;is the value that pushed to the stack.</p>
7-
8-
<p>The constructor requires a&nbsp;<code>stackSize</code>&nbsp;parameter, which represents the size of each stack.</p>
9-
10-
<p><strong>Example1:</strong></p>
11-
12-
<pre>
13-
<strong> Input</strong>:
14-
[&quot;TripleInOne&quot;, &quot;push&quot;, &quot;push&quot;, &quot;pop&quot;, &quot;pop&quot;, &quot;pop&quot;, &quot;isEmpty&quot;]
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-
[&quot;TripleInOne&quot;, &quot;push&quot;, &quot;push&quot;, &quot;push&quot;, &quot;pop&quot;, &quot;pop&quot;, &quot;pop&quot;, &quot;peek&quot;]
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&nbsp;<code>push(stackNum, value)</code>、<code>pop(stackNum)</code>、<code>isEmpty(stackNum)</code>、<code>peek(stackNum)</code>&nbsp;methods.&nbsp;<code>stackNum<font face="sans-serif, Arial, Verdana, Trebuchet MS">&nbsp;</font></code><font face="sans-serif, Arial, Verdana, Trebuchet MS">is the index of the stack.&nbsp;</font><code>value</code>&nbsp;is the value that pushed to the stack.</p>
9+
10+
11+
12+
<p>The constructor requires a&nbsp;<code>stackSize</code>&nbsp;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+
[&quot;TripleInOne&quot;, &quot;push&quot;, &quot;push&quot;, &quot;pop&quot;, &quot;pop&quot;, &quot;pop&quot;, &quot;isEmpty&quot;]
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+
[&quot;TripleInOne&quot;, &quot;push&quot;, &quot;push&quot;, &quot;push&quot;, &quot;pop&quot;, &quot;pop&quot;, &quot;pop&quot;, &quot;peek&quot;]
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+
```

lcci/03.01.Three in One/Solution.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class TripleInOne {
2+
private int[] s;
3+
private int capacity;
4+
5+
public TripleInOne(int stackSize) {
6+
s = new int[stackSize * 3 + 3];
7+
capacity = stackSize;
8+
}
9+
10+
public void push(int stackNum, int value) {
11+
if (s[stackNum + 3 * capacity] < capacity) {
12+
s[s[stackNum + 3 * capacity] * 3 + stackNum] = value;
13+
++s[stackNum + 3 * capacity];
14+
}
15+
}
16+
17+
public int pop(int stackNum) {
18+
if (isEmpty(stackNum)) {
19+
return -1;
20+
}
21+
--s[stackNum + 3 * capacity];
22+
return s[s[stackNum + 3 * capacity] * 3 + stackNum];
23+
}
24+
25+
public int peek(int stackNum) {
26+
return isEmpty(stackNum) ? -1 : s[(s[stackNum + 3 * capacity] - 1) * 3 + stackNum];
27+
}
28+
29+
public boolean isEmpty(int stackNum) {
30+
return s[stackNum + 3 * capacity] == 0;
31+
}
32+
}
33+
34+
/**
35+
* Your TripleInOne object will be instantiated and called as such: TripleInOne
36+
* obj = new TripleInOne(stackSize); obj.push(stackNum,value); int param_2 =
37+
* obj.pop(stackNum); int param_3 = obj.peek(stackNum); boolean param_4 =
38+
* obj.isEmpty(stackNum);
39+
*/

lcci/03.01.Three in One/Solution.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class TripleInOne:
2+
3+
def __init__(self, stackSize: int):
4+
self._capacity = stackSize
5+
self._s = [[], [], []]
6+
7+
def push(self, stackNum: int, value: int) -> None:
8+
if len(self._s[stackNum]) < self._capacity:
9+
self._s[stackNum].append(value)
10+
11+
def pop(self, stackNum: int) -> int:
12+
return -1 if self.isEmpty(stackNum) else self._s[stackNum].pop()
13+
14+
def peek(self, stackNum: int) -> int:
15+
return -1 if self.isEmpty(stackNum) else self._s[stackNum][-1]
16+
17+
def isEmpty(self, stackNum: int) -> bool:
18+
return len(self._s[stackNum]) == 0
19+
20+
21+
# Your TripleInOne object will be instantiated and called as such:
22+
# obj = TripleInOne(stackSize)
23+
# obj.push(stackNum,value)
24+
# param_2 = obj.pop(stackNum)
25+
# param_3 = obj.peek(stackNum)
26+
# param_4 = obj.isEmpty(stackNum)

0 commit comments

Comments
 (0)