-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathig_0305.cpp
More file actions
44 lines (39 loc) · 790 Bytes
/
ig_0305.cpp
File metadata and controls
44 lines (39 loc) · 790 Bytes
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
/**
* @file ig_0305.cpp
* @brief https://leetcode-cn.com/problems/sort-of-stacks-lcci/
* @author YongDu
* @date 2021-09-10
*/
class SortedStack {
public:
SortedStack() {}
void push(int val) {
if (stk1st.empty()) {
stk1st.push(val);
} else {
while (!stk1st.empty() && stk1st.top() < val) {
stk2nd.push(stk1st.top());
stk1st.pop();
}
stk1st.push(val);
while (!stk2nd.empty()) {
stk1st.push(stk2nd.top());
stk2nd.pop();
}
}
}
void pop() {
if (stk1st.empty())
return;
stk1st.pop();
}
int peek() {
if (stk1st.empty())
return -1;
return stk1st.top();
}
bool isEmpty() { return stk1st.empty(); }
private:
stack<int> stk1st;
stack<int> stk2nd;
};