劍指offer--面試題21--相關

題目:設計包含min函數的棧,pop(),push(),min()的時間複雜度均爲O(1)ios

 

本身所寫代碼以下:(寫‘棧’的代碼仍是有些不熟練!)函數

#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;
}

此題未考慮成熟!  須要的是輔助棧而非輔助變量!!!spa

 

修改以下:設計

//思路:首先,本身以前編寫的棧正是做者所說的第二種行不通的;
//此時,若深刻想下去,就可能由於須要更多的成員變量而想到再設置一個棧!

//解決本題的關鍵:想到設置輔助棧,並且經過例子來模擬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();
}
相關文章
相關標籤/搜索