Skip to content

Commit 0ec34e5

Browse files
committed
feat: add solutions to lcci question: 03.05.Sort of Stacks
添加《程序员面试金典》题解:面试题 03.05. 栈排序
1 parent 93a2aa7 commit 0ec34e5

File tree

4 files changed

+263
-54
lines changed

4 files changed

+263
-54
lines changed

lcci/03.05.Sort of Stacks/README.md

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,80 @@
3131

3232
## 解法
3333
<!-- 这里可写通用的实现逻辑 -->
34-
34+
利用辅助栈实现 `push` 操作,其余操作不变。
3535

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

3939
```python
40+
class SortedStack:
41+
42+
def __init__(self):
43+
self.s = []
44+
45+
def push(self, val: int) -> None:
46+
t = []
47+
while not self.isEmpty() and self.s[-1] < val:
48+
t.append(self.s.pop())
49+
self.s.append(val)
50+
while len(t) > 0:
51+
self.s.append(t.pop())
4052

53+
def pop(self) -> None:
54+
if not self.isEmpty():
55+
self.s.pop()
56+
57+
def peek(self) -> int:
58+
return -1 if self.isEmpty() else self.s[-1]
59+
60+
def isEmpty(self) -> bool:
61+
return len(self.s) == 0
4162
```
4263

4364
### Java
4465
<!-- 这里可写当前语言的特殊实现逻辑 -->
4566

4667
```java
47-
68+
class SortedStack {
69+
private Stack<Integer> s;
70+
public SortedStack() {
71+
s = new Stack<>();
72+
}
73+
74+
public void push(int val) {
75+
Stack<Integer> t = new Stack<>();
76+
while (!isEmpty() && s.peek() < val) {
77+
t.push(s.pop());
78+
}
79+
s.push(val);
80+
while (!t.isEmpty()) {
81+
s.push(t.pop());
82+
}
83+
}
84+
85+
public void pop() {
86+
if (!isEmpty()) {
87+
s.pop();
88+
}
89+
}
90+
91+
public int peek() {
92+
return isEmpty() ? -1 : s.peek();
93+
}
94+
95+
public boolean isEmpty() {
96+
return s.isEmpty();
97+
}
98+
}
99+
100+
/**
101+
* Your SortedStack object will be instantiated and called as such:
102+
* SortedStack obj = new SortedStack();
103+
* obj.push(val);
104+
* obj.pop();
105+
* int param_3 = obj.peek();
106+
* boolean param_4 = obj.isEmpty();
107+
*/
48108
```
49109

50110
### ...
Lines changed: 139 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,139 @@
1-
# [03.05. Sort of Stacks](https://leetcode-cn.com/problems/sort-of-stacks-lcci)
2-
3-
## Description
4-
<p>Write a program to sort a stack such that the smallest items are on the top. You can use an additional temporary stack, but you may not copy the elements into any other data structure (such as an array). The stack supports the following operations: <code>push</code>, <code>pop</code>, <code>peek</code>, and <code>isEmpty</code>. When the stack is empty, <code>peek</code> should return -1.</p>
5-
6-
<p><strong>Example1:</strong></p>
7-
8-
<pre>
9-
<strong> Input</strong>:
10-
[&quot;SortedStack&quot;, &quot;push&quot;, &quot;push&quot;, &quot;peek&quot;, &quot;pop&quot;, &quot;peek&quot;]
11-
[[], [1], [2], [], [], []]
12-
<strong> Output</strong>:
13-
[null,null,null,1,null,2]
14-
</pre>
15-
16-
<p><strong>Example2:</strong></p>
17-
18-
<pre>
19-
<strong> Input</strong>:
20-
[&quot;SortedStack&quot;, &quot;pop&quot;, &quot;pop&quot;, &quot;push&quot;, &quot;pop&quot;, &quot;isEmpty&quot;]
21-
[[], [], [], [1], [], []]
22-
<strong> Output</strong>:
23-
[null,null,null,null,null,true]
24-
</pre>
25-
26-
<p><strong>Note:</strong></p>
27-
28-
<ol>
29-
<li>The total number of elements in the stack is within the range [0, 5000].</li>
30-
</ol>
31-
32-
33-
34-
## Solutions
35-
36-
37-
### Python3
38-
39-
```python
40-
41-
```
42-
43-
### Java
44-
45-
```java
46-
47-
```
48-
49-
### ...
50-
```
51-
52-
```
1+
# [03.05. Sort of Stacks](https://leetcode-cn.com/problems/sort-of-stacks-lcci)
2+
3+
## Description
4+
<p>Write a program to sort a stack such that the smallest items are on the top. You can use an additional temporary stack, but you may not copy the elements into any other data structure (such as an array). The stack supports the following operations: <code>push</code>, <code>pop</code>, <code>peek</code>, and <code>isEmpty</code>. When the stack is empty, <code>peek</code> should return -1.</p>
5+
6+
7+
8+
<p><strong>Example1:</strong></p>
9+
10+
11+
12+
<pre>
13+
14+
<strong> Input</strong>:
15+
16+
[&quot;SortedStack&quot;, &quot;push&quot;, &quot;push&quot;, &quot;peek&quot;, &quot;pop&quot;, &quot;peek&quot;]
17+
18+
[[], [1], [2], [], [], []]
19+
20+
<strong> Output</strong>:
21+
22+
[null,null,null,1,null,2]
23+
24+
</pre>
25+
26+
27+
28+
<p><strong>Example2:</strong></p>
29+
30+
31+
32+
<pre>
33+
34+
<strong> Input</strong>:
35+
36+
[&quot;SortedStack&quot;, &quot;pop&quot;, &quot;pop&quot;, &quot;push&quot;, &quot;pop&quot;, &quot;isEmpty&quot;]
37+
38+
[[], [], [], [1], [], []]
39+
40+
<strong> Output</strong>:
41+
42+
[null,null,null,null,null,true]
43+
44+
</pre>
45+
46+
47+
48+
<p><strong>Note:</strong></p>
49+
50+
51+
52+
<ol>
53+
54+
<li>The total number of elements in the stack is within the range [0, 5000].</li>
55+
56+
</ol>
57+
58+
59+
60+
61+
## Solutions
62+
63+
64+
### Python3
65+
66+
```python
67+
class SortedStack:
68+
69+
def __init__(self):
70+
self.s = []
71+
72+
def push(self, val: int) -> None:
73+
t = []
74+
while not self.isEmpty() and self.s[-1] < val:
75+
t.append(self.s.pop())
76+
self.s.append(val)
77+
while len(t) > 0:
78+
self.s.append(t.pop())
79+
80+
def pop(self) -> None:
81+
if not self.isEmpty():
82+
self.s.pop()
83+
84+
def peek(self) -> int:
85+
return -1 if self.isEmpty() else self.s[-1]
86+
87+
def isEmpty(self) -> bool:
88+
return len(self.s) == 0
89+
```
90+
91+
### Java
92+
93+
```java
94+
class SortedStack {
95+
private Stack<Integer> s;
96+
public SortedStack() {
97+
s = new Stack<>();
98+
}
99+
100+
public void push(int val) {
101+
Stack<Integer> t = new Stack<>();
102+
while (!isEmpty() && s.peek() < val) {
103+
t.push(s.pop());
104+
}
105+
s.push(val);
106+
while (!t.isEmpty()) {
107+
s.push(t.pop());
108+
}
109+
}
110+
111+
public void pop() {
112+
if (!isEmpty()) {
113+
s.pop();
114+
}
115+
}
116+
117+
public int peek() {
118+
return isEmpty() ? -1 : s.peek();
119+
}
120+
121+
public boolean isEmpty() {
122+
return s.isEmpty();
123+
}
124+
}
125+
126+
/**
127+
* Your SortedStack object will be instantiated and called as such:
128+
* SortedStack obj = new SortedStack();
129+
* obj.push(val);
130+
* obj.pop();
131+
* int param_3 = obj.peek();
132+
* boolean param_4 = obj.isEmpty();
133+
*/
134+
```
135+
136+
### ...
137+
```
138+
139+
```
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class SortedStack {
2+
private Stack<Integer> s;
3+
public SortedStack() {
4+
s = new Stack<>();
5+
}
6+
7+
public void push(int val) {
8+
Stack<Integer> t = new Stack<>();
9+
while (!isEmpty() && s.peek() < val) {
10+
t.push(s.pop());
11+
}
12+
s.push(val);
13+
while (!t.isEmpty()) {
14+
s.push(t.pop());
15+
}
16+
}
17+
18+
public void pop() {
19+
if (!isEmpty()) {
20+
s.pop();
21+
}
22+
}
23+
24+
public int peek() {
25+
return isEmpty() ? -1 : s.peek();
26+
}
27+
28+
public boolean isEmpty() {
29+
return s.isEmpty();
30+
}
31+
}
32+
33+
/**
34+
* Your SortedStack object will be instantiated and called as such:
35+
* SortedStack obj = new SortedStack();
36+
* obj.push(val);
37+
* obj.pop();
38+
* int param_3 = obj.peek();
39+
* boolean param_4 = obj.isEmpty();
40+
*/

lcci/03.05.Sort of Stacks/Solution.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class SortedStack:
2+
3+
def __init__(self):
4+
self.s = []
5+
6+
def push(self, val: int) -> None:
7+
t = []
8+
while not self.isEmpty() and self.s[-1] < val:
9+
t.append(self.s.pop())
10+
self.s.append(val)
11+
while len(t) > 0:
12+
self.s.append(t.pop())
13+
14+
def pop(self) -> None:
15+
if not self.isEmpty():
16+
self.s.pop()
17+
18+
def peek(self) -> int:
19+
return -1 if self.isEmpty() else self.s[-1]
20+
21+
def isEmpty(self) -> bool:
22+
return len(self.s) == 0

0 commit comments

Comments
 (0)