题目:实现一个特殊的栈,在实现栈的基本操作的基础上,再实现返回栈中最小元素的操作。
要求:①pop、push、getMin操作的时间复杂度都为O(1)。②设计的栈类型可以使用现成的栈结构
分析:该题为左程云《程序员代码面试指南》第一章的题,难度为★。思路是使用两个栈,一个栈是正常栈,另一个栈用于保存每一步的最小值。对数器中的标准方法采用对栈的遍历找到最小值,该方法时间复杂度为O(N)。无需测试用例,使用方便。
#include <stack>
#include <iostream>
#include <time.h>
using namespace std;
int srandNum1 = 0;
int srandNum2 = 100;
bool forTest = false;
class StackWithGetmin
{
public:
void pop();
void push(int obj);
bool empty();
int top();
int size();
int getMin();
private:
stack<int> standardStack;
stack<int> getminStack;
};
bool StackWithGetmin::empty()
{
if (standardStack.empty()) return true;
else return false;
}
int StackWithGetmin::size()
{
return standardStack.size();
}
void StackWithGetmin::push(int obj)
{
standardStack.push(obj);
if (getminStack.size()==0)
{
getminStack.push(obj);
}
else
{
if (obj<=getminStack.top())
{
getminStack.push(obj);
}
}
}
void StackWithGetmin::pop()
{
if (standardStack.top()==getminStack.top())
{
getminStack.pop();
}
standardStack.pop();
}
int StackWithGetmin::top()
{
return standardStack.top();
}
int StackWithGetmin::getMin()
{
return getminStack.top();
}
void printStack(stack<int> stk);
stack<int> generateRandomStack(int maxSize, int maxValue, int &size);
StackWithGetmin transformRandomStack(stack<int> stk);
stack<int> copyStack(stack<int> stk);
int comarator(stack<int> stk);
bool isEqual(int num1, int num2);
int main()
{
if (!forTest)
{
int size = 0;
int testTime = 50000;
int maxSize = 10;
int maxValue = 10;
bool succeed = true;
for (int i = 0; i < testTime; i++)
{
stack<int> stk1 = generateRandomStack(maxSize, maxValue, size);
stack<int> stk2 = copyStack(stk1);
StackWithGetmin stkS = transformRandomStack(stk1);
int num1 = stkS.getMin();
int num2 = comarator(stk2);
if (!isEqual(num1, num2))
{
succeed = false;
printStack(stk1);
cout << "num1 = " << num1 << endl;
cout << "num2 = " << num2 << endl;
break;
}
}
if (succeed)
{
cout << "Nice!" << endl;
}
else
{
cout << "Fucking Fucked" << endl;
}
}
else
{
//test
}
system("pause");
return 0;
}
stack<int> generateRandomStack(int maxSize, int maxValue, int &size)
{
srandNum1 += 3;
srandNum2++;
srand((unsigned)time(NULL)*srandNum1*srandNum1*srandNum2*srandNum2*srandNum2);
size = (int)(rand() % (maxSize)+1);
stack<int> stk;
for (int i = 0; i < size; i++)
{
stk.push(rand() % maxValue);
}
//printStack();
return stk;
}
StackWithGetmin transformRandomStack(stack<int> stk)
{
stack<int> tmp1 = stk;
stack<int> tmp2;
StackWithGetmin stkDst;
while (!tmp1.empty())
{
tmp2.push(tmp1.top());
tmp1.pop();
}
while (!tmp2.empty())
{
stkDst.push(tmp2.top());
tmp2.pop();
}
return stkDst;
}
void printStack(stack<int> stk)
{
cout << "top->bottom : ";
stack<int> tmp = stk;
while (!tmp.empty())
{
cout << tmp.top() << " ";
tmp.pop();
}
cout << endl;
}
stack<int> copyStack(stack<int> stk)
{
stack<int> newStack = stk;
return newStack;
}
int comarator(stack<int> stk)
{
stack<int> tmp = stk;
int min = 1000;
while (!tmp.empty())
{
min = tmp.top() < min ? tmp.top() : min;
tmp.pop();
}
return min;
}
bool isEqual(int num1,int num2)
{
return num1 == num2 ? true : false;
}