-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathO(1)minStack.cpp
More file actions
74 lines (63 loc) · 1.39 KB
/
Copy pathO(1)minStack.cpp
File metadata and controls
74 lines (63 loc) · 1.39 KB
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
// 实现一个特殊的栈,在实现栈的基本功能的基础上,再返回栈中最小元素的操作
// 要求 1 pop push getMin操作的时间复杂度都是O(1)
// 设计的栈类型可以使用现成的栈结构(栈的实现结构:双向链表结构 / 数组)
/**
* 思想:建立两个栈 1数据栈 2最小栈,每当数据栈有数据压入时将该数据与最小栈栈顶元素进行比较,将较小的值同步压入最小栈
* 弹出时两个栈也同步弹出
*/
#include <stack>
#include <iostream>
#include <algorithm>
using namespace std;
class MyStack{
private:
stack<int> dataStack;
stack<int> minStack;
public:
MyStack(){
}
void push(int obj){
dataStack.push(obj);
if (minStack.empty()){
minStack.push(obj);
}
else {
minStack.push(min(this->getMin(), obj));
}
}
void pop(){
minStack.pop();
dataStack.pop();
}
int top() {
return dataStack.top();
}
int getMin(){
if (!minStack.empty()){
return minStack.top();
}
else{
cerr << "your stack is empty";
}
}
int getSize(){
return dataStack.size();
}
};
int main(){
MyStack s1;
s1.push(1);
s1.push(3);
s1.push(6);
cout <<"top is :"<< s1.top() << endl;
for (int i = 0; i < s1.getSize(); i++){
s1.pop();
}
for (int i = 0; i < 15; i++){
s1.push(i % 5);
}
cout << "top is :" << s1.top() << endl;
cout << "min is:"<<s1.getMin() << endl;
system("pause");
return 0;
}