forked from zhedahht/CodingInterviewChinese2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinInStack.cpp
More file actions
63 lines (46 loc) · 1.44 KB
/
MinInStack.cpp
File metadata and controls
63 lines (46 loc) · 1.44 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
/*******************************************************************
Copyright(c) 2016, Harry He
All rights reserved.
Distributed under the BSD license.
(See accompanying file LICENSE.txt at
https://github.com/zhedahht/CodingInterviewChinese2/blob/master/LICENSE.txt)
*******************************************************************/
//==================================================================
// 《剑指Offer——名企面试官精讲典型编程题》代码
// 作者:何海涛
//==================================================================
// 面试题30:包含min函数的栈
// 题目:定义栈的数据结构,请在该类型中实现一个能够得到栈的最小元素的min
// 函数。在该栈中,调用min、push及pop的时间复杂度都是O(1)。
#include <cstdio>
#include "StackWithMin.h"
void Test(const char* testName, const StackWithMin<int>& stack, int expected)
{
if(testName != nullptr)
printf("%s begins: ", testName);
if(stack.min() == expected)
printf("Passed.\n");
else
printf("Failed.\n");
}
int main(int argc, char* 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;
}