剑指offer--面试题21--相关

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

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

#include <iostream>

using namespace std;

const int MAX = 100;

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 = 0;
    minvalue = INT_MAX;
    minSecvalue = INT_MAX;
}

Stack::~Stack()
{
}

bool Stack::empty() const
{
    return topindex == 0;
}

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=1; i<=10; 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(2);
    top = st.top();
    cout<<top<<endl;
    min = st.min();
    cout<<min<<endl;

    return 0;
}

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

修改如下:

//思路:首先,自己之前编写的栈正是作者所说的第二种行不通的;
//此时,若深入想下去,就可能因为需要更多的成员变量而想到再设置一个栈!

//解决本题的关键:想到设置辅助栈,而且通过例子来模拟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();
}
清醒时做事,糊涂时读书,大怒时睡觉,独处时思考; 做一个幸福的人,读书,旅行,努力工作,关心身体和心情,成为最好的自己 -- 共勉
原文地址:https://www.cnblogs.com/hello-yz/p/3257286.html