forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
44 lines (38 loc) · 833 Bytes
/
Solution.cpp
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
class SortedStack {
public:
SortedStack() {
}
void push(int val) {
stack<int> t;
while (!stk.empty() && stk.top() < val) {
t.push(stk.top());
stk.pop();
}
stk.push(val);
while (!t.empty()) {
stk.push(t.top());
t.pop();
}
}
void pop() {
if (!isEmpty()) {
stk.pop();
}
}
int peek() {
return isEmpty() ? -1 : stk.top();
}
bool isEmpty() {
return stk.empty();
}
private:
stack<int> stk;
};
/**
* Your SortedStack object will be instantiated and called as such:
* SortedStack* obj = new SortedStack();
* obj->push(val);
* obj->pop();
* int param_3 = obj->peek();
* bool param_4 = obj->isEmpty();
*/