zoukankan      html  css  js  c++  java
  • 算法复习:栈和队列

    用两个栈实现队列,用两个队列实现栈都要会

    leetcode 面试题09. 用两个栈实现队列

    #include<stack>
    class CQueue {
    public:
        stack<int> in;
        stack<int> out;
        int state=0;
        CQueue() { }
        void appendTail(int value) {
            if(state==1)
            {
                while(out.size())
                {
                    in.push(out.top());
                    out.pop();
                }
                state=0;
            }
            in.push(value);
            return;
        }
        
        int deleteHead() {
            if(state==0)
            {
                while(in.size())
                {
                    out.push(in.top());
                    in.pop();
                }
                state=1;
            }
            if(!out.size())
                return -1;
            int output=out.top();
            out.pop();
            return output;
        }
    };
    
    /**
     * Your CQueue object will be instantiated and called as such:
     * CQueue* obj = new CQueue();
     * obj->appendTail(value);
     * int param_2 = obj->deleteHead();
     */
    leetcode s9

     leetcode 232. 用栈实现队列

    要求不能用stack,用list代替stack

    注意:list相当于双向链表,基本操作:push_back() back() pop_back()  && push_front() front() pop_front()  && size() clear()

    stack的基本操作:size( ) :返回栈中元素个数、top( ) :返回栈顶的元素、pop( ) :从栈顶取出并删除元素、push(e) :向栈顶添加元素e、empty( ) :栈为空时返回true

    #include<list>
    class MyQueue {
    public:
        list<int> in;
        list<int> out;
        int state=0;
        /** Initialize your data structure here. */
        MyQueue() { }
        /** Push element x to the back of queue. */
        void push(int x) {
            if(state==1)
            {
                while(out.size())
                {
                    in.push_back(out.back());
                    out.pop_back();
                }
                state=0;
            }
            in.push_back(x);
            return;
        }
        
        /** Removes the element from in front of queue and returns that element. */
        int pop() {
            if(state==0)
            {
                while(in.size())
                {
                    out.push_back(in.back());
                    in.pop_back();
                }
                state=1;
            }
            if(!out.size())
                return -1;
            int output=out.back();
            out.pop_back();
            return output;
        }
        
        /** Get the front element. */
        int peek() {
            if(state==0)
            {
                while(in.size())
                {
                    out.push_back(in.back());
                    in.pop_back();
                }
                state=1;
            }
            if(!out.size())
                return -1;
            return out.back();
        }
        
        /** Returns whether the queue is empty. */
        bool empty() {
            return (out.size()+in.size()==0);
        }
    };
    
    /**
     * Your MyQueue object will be instantiated and called as such:
     * MyQueue* obj = new MyQueue();
     * obj->push(x);
     * int param_2 = obj->pop();
     * int param_3 = obj->peek();
     * bool param_4 = obj->empty();
     */
    leetcode 232

    leetcode 225. 用队列实现栈

    队列queue的基本操作:front()显示队头、back()显示队尾、pop()删除队头、push()队尾插入、size()

    #include<queue>
    class MyStack {
    public:
        queue<int> in;
        queue<int> out;
        /** Initialize your data structure here. */
        MyStack() { }
        int state=0;
        /** Push element x onto stack. */
        void push(int x) {
            if(state==1)
            {
                while(out.size())
                {
                    in.push(out.front());
                    out.pop();
                }
                state=0;
            }
            in.push(x);
            return;
        }
        
        /** Removes the element on top of the stack and returns that element. */
        int pop() {
            if(state==1)
            {
                while(out.size())
                {
                    in.push(out.front());
                    out.pop();
                }
                state=0;
            }
            while(in.size()>1)
            {
                out.push(in.front());
                in.pop();
            }
            if(in.size()==0)
                return -1;
            state=1;
            int output=in.front();
            in.pop();
            return output;
        }
        
        /** Get the top element. */
        int top() {
            if(state==1)
            {
                while(out.size())
                {
                    in.push(out.front());
                    out.pop();
                }
                state=0;
            }
            while(in.size()>1)
            {
                out.push(in.front());
                in.pop();
            }
            if(in.size()==0)
                return -1;
            state=1;
            int output=in.front();
            out.push(in.front());
            in.pop();
            return output;
        }
        
        /** Returns whether the stack is empty. */
        bool empty() {
            return (in.size()+out.size()==0);
        }
    };
    
    /**
     * Your MyStack object will be instantiated and called as such:
     * MyStack* obj = new MyStack();
     * obj->push(x);
     * int param_2 = obj->pop();
     * int param_3 = obj->top();
     * bool param_4 = obj->empty();
     */
    leetcode 225
  • 相关阅读:
    【转载】Highcharts一些属性
    What is assembly?
    用Apache配置Git服务器
    【转】.NET试题总结二
    【转】SVN服务器的快速搭建。
    【转】.NET试题总结一
    【转】国外C#开源系统一览表 ,C# Open Source
    Amazon S3 REST方式获取Object
    Action Filter
    a 标签 name 熟悉因为头部固定,导致置顶遮挡解决方案
  • 原文地址:https://www.cnblogs.com/dzzy/p/12312601.html
Copyright © 2011-2022 走看看