-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinInStack.cpp
More file actions
54 lines (40 loc) · 1.06 KB
/
MinInStack.cpp
File metadata and controls
54 lines (40 loc) · 1.06 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
// 面试题30:包含min函数的栈
// 题目:定义栈的数据结构,请在该类型中实现一个能够得到栈的最小元素的min
// 函数。在该栈中,调用min、push及pop的时间复杂度都是O(1)。
/*
同leetcode 155 最小栈 https://leetcode-cn.com/problems/min-stack/
*/
#include<cstdio>
#include "StackWithMin.h"
void Test(const char* testName,const StackWithMin<int>& stack,int expected)
{
if(testName != nullptr)
printf("%s,beigin:",testName);
if(stack.min() == expected)
printf("Passed.\n");
else
{
printf("Failed.\n");
}
}
int main(int argc, char const *argv[])
{
StackWithMin<int> stack;
stack.push(3);
Test("Test1", stack, 3);
stack.push(4);
Test("Test2", stack, 3);
stack.push(2);
Test("Test3", stack, 2);
stack.push(3);
Test("Test4", stack, 2);
stack.pop();
Test("Test5", stack, 2);
stack.pop();
Test("Test6", stack, 3);
stack.pop();
Test("Test7", stack, 3);
stack.push(0);
Test("Test8", stack, 0);
return 0;
}