-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinStack.cpp
More file actions
44 lines (40 loc) · 838 Bytes
/
MinStack.cpp
File metadata and controls
44 lines (40 loc) · 838 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
class MinStack {
public:
MinStack() {
// do intialization if necessary
}
/*
* @param number: An integer
* @return: nothing
*/
void push(int number) {
// write your code here
m_stkSrc.push(number);
int minnum = number;
if (!m_stkOrder.empty() && m_stkOrder.top() < minnum)
{
minnum = m_stkOrder.top();
}
m_stkOrder.push(minnum);
}
/*
* @return: An integer
*/
int pop() {
// write your code here
int top = m_stkSrc.top();
m_stkSrc.pop();
m_stkOrder.pop();
return top;
}
/*
* @return: An integer
*/
int min() {
// write your code here
return m_stkOrder.top();
}
private:
stack<int> m_stkSrc;
stack<int> m_stkOrder;
};