-
Notifications
You must be signed in to change notification settings - Fork 8
/
SortOfStacksLCCI.java
109 lines (90 loc) · 2.54 KB
/
SortOfStacksLCCI.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package stack;
// Source : https://leetcode.com/problems/sort-of-stacks-lcci/
// Id : mst03.05
// Author : Fanlu Hai | https://github.com/Fanlu91/FanluLeetcode
// Date : 2022/2/9
// Topic : stack
// Level : Medium-
// Other :
// Tips :
// Links :
// Result : 99.82% 10.49%
import java.util.Deque;
import java.util.LinkedList;
public class SortOfStacksLCCI {
// 非惰性解法 160 ms
// 惰性解法 12 ms
Deque<Integer> stack = new LinkedList<>(), tmp = new LinkedList<>();
// 非惰性解法 844 ms
// 惰性解法 13 ms
// Stack<Integer> stack = new Stack<>(), tmp = new Stack<>();
public SortOfStacksLCCI() {
// public SortedStack() {
}
/*public void push(int val) {
if (isEmpty())
stack.push(val);
else {
int top = stack.peek();
while (top < val) {
tmp.push(stack.pop());
if (stack.isEmpty())
break;
top = stack.peek();
}
stack.push(val);
while (!tmp.isEmpty()) {
stack.push(tmp.pop());
}
}
}
public void pop() {
if (!stack.isEmpty())
stack.pop();
}
public int peek() {
if (stack.isEmpty())
return -1;
return stack.peek();
}
public boolean isEmpty() {
return stack.isEmpty();
}*/
/**
* 惰性更新
*/
public void push(int val) {
int minBig = stack.isEmpty() ? Integer.MAX_VALUE : stack.peek();
int maxSmall = tmp.isEmpty() ? Integer.MIN_VALUE : tmp.peek();
while (true) {
if (val > minBig) {
tmp.push(stack.pop());
minBig = stack.isEmpty() ? Integer.MAX_VALUE : stack.peek();
} else if (val < maxSmall) {
stack.push(tmp.pop());
maxSmall = tmp.isEmpty() ? Integer.MIN_VALUE : tmp.peek();
} else { // maxSmall<val<minBig
stack.push(val);
break;
}
}
}
public void pop() {
//将辅助栈元素push回原始栈
while (!tmp.isEmpty()) {
stack.push(tmp.pop());
}
if (!stack.isEmpty())
stack.pop();
}
public int peek() {
//将辅助栈元素push回原始栈
while (!tmp.isEmpty()) {
stack.push(tmp.pop());
}
return stack.isEmpty() ? -1 : stack.peek();
}
public boolean isEmpty() {
return stack.isEmpty() && tmp.isEmpty();
}
}