题目:设计包含min函数的栈,pop(),push(),min()的时间复杂度均为O(1)

自己所写代码如下:(写‘栈’的代码还是有些不熟练!)

#include <iostream>

using namespace std;

const int MAX = ;

class Stack
{
private:
int values[MAX];
int topindex;
int minvalue;
int minSecvalue; public:
Stack();
virtual ~Stack(); int top() const;
void push(int n);
void pop();
int min();
bool empty() const;
}; Stack::Stack()
{
topindex = ;
minvalue = INT_MAX;
minSecvalue = INT_MAX;
} Stack::~Stack()
{
} bool Stack::empty() const
{
return topindex == ;
} int Stack::top() const
{
int toptemp = topindex;
if(!empty())
{
--toptemp;
return values[toptemp];
}
else
{
cerr<<"Stack is empty!"<<endl;
return INT_MAX;
}
} void Stack::push(int n)
{
if(topindex < MAX)
{
values[topindex++] = n;
if(minvalue > n)
{
minSecvalue = minvalue;
minvalue = n;
}
}
else
cerr<<"Stack is full!"<<endl;
} void Stack::pop()
{
if(!empty())
{
topindex--;
if(values[topindex] == minvalue)
minvalue = minSecvalue;
}
else
cerr<<"Stack is empty!"<<endl;
} int Stack::min()
{
if(!empty())
return minvalue;
else
{
cerr<<"Stack is empty!"<<endl;
return INT_MAX;
}
}
#include "stdafx.h"
#include <iostream>
#include "Stack.h" using namespace std; int main()
{
Stack st;
for(int i=; i<=; i++)
st.push(i);
int top = st.top();
cout<<top<<endl;
int min = st.min();
cout<<min<<endl;
st.pop();
st.pop();
top = st.top();
cout<<top<<endl;
st.push();
top = st.top();
cout<<top<<endl;
min = st.min();
cout<<min<<endl; return ;
}

此题未考虑成熟!  需要的是辅助栈而非辅助变量!!!

修改如下:

//思路:首先,自己之前编写的栈正是作者所说的第二种行不通的;
//此时,若深入想下去,就可能因为需要更多的成员变量而想到再设置一个栈! //解决本题的关键:想到设置辅助栈,而且通过例子来模拟push,pop和min函数
// 关键中的关键:min函数的操作如何达到O(1),或者说是辅助栈的入栈与出栈 //默写代码如下:
//利用stl中的stack,并将StackWithMin写成类模板
#include<stack> template<class T>
class StackWithMin
{
private:
std::stack<T> stmain;
std::stack<T> sthelp;
public:
StackWithMin();
virtual ~StackWithMin(); bool empty() const;
T min() const;
void pop();
void push(const T& n);
T top() const;
size_t size() const;
}; template<class T>
StackWithMin<T>::StackWithMin()
{
} template<class T>
StackWithMin<T>::~StackWithMin()
{
} template<class T>
bool StackWithMin<T>::empty() const
{
return stmain.empty();
} template<class T>
T StackWithMin<T>::top() const
{
return stmain.top();
} template<class T>
void StackWithMin<T>::push(const T& n)
{
if(sthelp.empty())
{
sthelp.push(n);
stmain.push(n);
}
else
{
stmain.push(n);
T Intosthelp = (n < sthelp.top()) ? n : sthelp.top();
sthelp.push(Intosthelp);
}
} template<class T>
void StackWithMin<T>::pop()
{
if(!stmain.empty() && !sthelp.empty())
{
stmain.pop();
sthelp.pop();
}
else
std::cout<<"Stack is empty!"<<std::endl;
} template<class T>
T StackWithMin<T>::min() const
{
return sthelp.top();
} template<class T>
size_t StackWithMin<T>::size() const
{
return stmain.size();
}

剑指offer--面试题21--相关的更多相关文章

  1. 剑指Offer:面试题21——包含min函数的栈(java实现)

    问题描述: 定义栈的数据结构,请在该类型中实现一个能够得到栈的最小元素的min函数.在该栈中,调用min,push及pop的时间复杂度都是O(1). 思路:加入一个辅助栈用来存储最小值集合 (这里要注 ...

  2. 【剑指offer 面试题21】包含min函数的栈

    思路: 通过增加一个辅助栈保存每个状态对应的最小值.栈实现的不完整,应该还包含empty()等常规函数. #include <iostream> #include <stack> ...

  3. C++版 - 剑指Offer 面试题45:圆圈中最后剩下的数字(约瑟夫环问题,ZOJ 1088:System Overload类似)题解

    剑指Offer 面试题45:圆圈中最后剩下的数字(约瑟夫环问题) 原书题目:0, 1, - , n-1 这n个数字排成一个圈圈,从数字0开始每次从圆圏里删除第m个数字.求出这个圈圈里剩下的最后一个数字 ...

  4. 剑指Offer:面试题15——链表中倒数第k个结点(java实现)

    问题描述 输入一个链表,输出该链表中倒数第k个结点.(尾结点是倒数第一个) 结点定义如下: public class ListNode { int val; ListNode next = null; ...

  5. 剑指offer面试题3 二维数组中的查找(c)

    剑指offer面试题三:

  6. 剑指Offer——笔试题+知识点总结

    剑指Offer--笔试题+知识点总结 情景回顾 时间:2016.9.23 12:00-14:00 19:00-21:00 地点:山东省网络环境智能计算技术重点实验室 事件:笔试 注意事项:要有大局观, ...

  7. C++版 - 剑指offer之面试题37:两个链表的第一个公共结点[LeetCode 160] 解题报告

    剑指offer之面试题37 两个链表的第一个公共结点 提交网址: http://www.nowcoder.com/practice/6ab1d9a29e88450685099d45c9e31e46?t ...

  8. C++版 - 剑指offer 面试题23:从上往下打印二叉树(二叉树的层次遍历BFS) 题解

    剑指offer  面试题23:从上往下打印二叉树 参与人数:4853  时间限制:1秒  空间限制:32768K 提交网址: http://www.nowcoder.com/practice/7fe2 ...

  9. C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解

    剑指offer 面试题39:判断平衡二叉树 提交网址:  http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...

  10. Leetcode - 剑指offer 面试题29:数组中出现次数超过一半的数字及其变形(腾讯2015秋招 编程题4)

    剑指offer 面试题29:数组中出现次数超过一半的数字 提交网址: http://www.nowcoder.com/practice/e8a1b01a2df14cb2b228b30ee6a92163 ...

随机推荐

  1. JMeter2.13 连接 sql server

    1.安装驱动 http://www.microsoft.com/zh-CN/download/details.aspx?id=11774 下载最新的即可 解压后复制势穷力竭sqljdbc.jar到 “ ...

  2. php的register_shutdown_function函数详解

    function shutdown() { $last_error = error_get_last(); if ($last_error) { error_log(print_r($last_err ...

  3. [Linux] Ubuntu Server 12.04 LTS 平台上搭建WordPress(Nginx+MySQL+PHP) Part IV

    接下来我们去下载 WorePress 用最新的 3.7.1 下载地址是:http://cn.wordpress.org/wordpress-3.7.1-zh_CN.zip 我们先建立一个文件夹 /va ...

  4. delphi 类型转化

    1.typecasting类型强制转化 var B : Boolean; Begin B := Boolean(1); End; 对于对象和接口,采用as操作符进行转化,但要先进行兼容性判断. 2.P ...

  5. SRF之数据字典

      框架提供数据字典的配置和显示的功能 字典以编码作为标识,用varchar(50)类型保存字典的编码.   字典的用法 1.在代码里边需要查询字典信息的 可用 Components.DataDict ...

  6. 解决matplotlib中文乱码问题(Windows)

    1.修改matplotlibrc文件 进入Python安装目录下的Lib\site-packages\matplotlib\mpl-data目录,打开matplotlibrc文件,删除font.fam ...

  7. Map和HashMap

    通过查询JDK帮助文档,我们可以得知Map的说明.方法等 import java.util.Map; import java.util.HashMap; class Test{ public stat ...

  8. java读取各类型的文件

    java读取各类型的文件 用到的几个包 bcmail-jdk14-132.jar/bcprov-jdk14-132.jar/checkstyle-all-4.2.jar/FontBox-0.1.0-d ...

  9. uva 11186 Circum Triangle<叉积>

    链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...

  10. Mongodb shell 基本操作

    /opt/mongodb-2.6.6/bin > mongo 1. 查询本地所有数据库名称> show dbs 2. 切换至指定数据库环境(若无指定的数据库,则创建新的库)> use ...