leetcode系列(9)miniStack,stack_to_queue, queue_to_stack

原创 2015年07月07日 21:32:08

由于种种原因没有做leetcode,博客也写的少了,这是个不好的现象,不能找借口,继续学习和写博客,这两天晚上新申请了leetcode账户(gmail在中国太难用了),重新做起,坚持每个题目都用C++和python撸一下。好久没有做题了从软柿子开始,所以就碰到了这三个题目:

1. mini stack

2. implement queue using stacks

3. implement stack using queues

三个题目都是一样的思路用辅助stack或者queue,mini stack会加一个辅助stack保持所有的最小值,queue和stack相互实现其实就是两个stack或者两个queue倒来倒去,唯一不同是queue实现stack每次pop或者top都是倒一次,直接看代码吧:

mini stack C++

class MinStack {
public:
    void push(int x) {
        stack_.push(x);
        if (mini_stack_.empty() || mini_stack_.top() >= x) {
            mini_stack_.push(x);
        }
    }

    void pop() {
        if (stack_.top() == mini_stack_.top()) {
            mini_stack_.pop();
        }
        stack_.pop();
    }

    int top() {
        assert(!stack_.empty());
        return stack_.top();
    }

    int getMin() {
        assert(!mini_stack_.empty());
        return mini_stack_.top();
    }
private:
    std::stack<int> stack_;
    std::stack<int> mini_stack_;
};

mini stack Python

class MinStack:
    # initialize your data structure here.
    def __init__(self):
        self.stack = []
        self.mini_stack = []

    # @param x, an integer
    # @return nothing
    def push(self, x):
        self.stack.append(x)
        if not self.mini_stack or self.mini_stack[-1] >= x:
            self.mini_stack.append(x)

    # @return nothing
    def pop(self):
        if self.stack[-1] == self.mini_stack[-1]:
            self.mini_stack.pop()
        self.stack.pop()

    # @return an integer
    def top(self):
        assert self.stack != [], "stack is empty"
        return self.stack[-1]

    # @return an integer
    def getMin(self):
        assert self.mini_stack != [], "mini stack is empty"
        return self.mini_stack[-1]

implement queue using stack C++

class Queue {
public:
    // Push element x to the back of queue.
    void push(int x) {
        stack_.push(x);
    }

    // Removes the element from in front of queue.
    void pop(void) {
        assert(!empty());
        if (helper_stack_.empty()) {
            _dump_helper_stack();
        }
        helper_stack_.pop();
    }

    // Get the front element.
    int peek(void) {
        assert(!empty());
        if (helper_stack_.empty()) {
            _dump_helper_stack();
        }
        return helper_stack_.top();
    }

    // Return whether the queue is empty.
    bool empty(void) {
        return stack_.empty() && helper_stack_.empty();
    }
private:
    std::stack<int> stack_;
    std::stack<int> helper_stack_;
    void _dump_helper_stack() {
        while (!stack_.empty()) {
            helper_stack_.push(stack_.top());
            stack_.pop();
        }
    }
};
implement queue using stacks Python

class Queue:
    # initialize your data structure here.
    def __init__(self):
        self.stack = []
        self.helper_stack = []

    # @param x, an integer
    # @return nothing
    def push(self, x):
        self.stack.append(x)

    # @return nothing
    def pop(self):
        assert not self.empty(), "both stack are empty"
        if not self.helper_stack:
            self._dump_helper_stack()
        self.helper_stack.pop()

    # @return an integer
    def peek(self):
        assert not self.empty(), "both stack are empty"
        if not self.helper_stack:
            self._dump_helper_stack()
        return self.helper_stack[-1]

    # @return an boolean
    def empty(self):
        return (not self.stack) and (not self.helper_stack)
    
    def _dump_helper_stack(self):
        while (len(self.stack) != 0):
                self.helper_stack.append(self.stack[-1])
                self.stack.pop()

implement stack using queues C++

class Stack {
public:
    // Push element x onto stack.
    void push(int x) {
        if (empty()) {
            que_1_.push_back(x);
        } else if (que_1_.empty()) {
            que_2_.push_back(x);
        } else {
            que_1_.push_back(x);
        }
    }

    // Removes the element on top of the stack.
    void pop() {
        assert(!empty());
        if (que_2_.empty()) {
            while (que_1_.size() > 1) {
                que_2_.push_back(que_1_.front());
                que_1_.pop_front();
            }
            que_1_.pop_front();
        } else {
            while (que_2_.size() > 1) {
                que_1_.push_back(que_2_.front());
                que_2_.pop_front();
            }
            que_2_.pop_front();
        }
    }

    // Get the top element.
    int top() {
        assert(!empty());
        int ret;
        if (que_2_.empty()) {
            while (que_1_.size() > 1) {
                que_2_.push_back(que_1_.front());
                que_1_.pop_front();
            }
            ret = que_1_.front();
            que_2_.push_back(ret);
            que_1_.pop_front();
        } else {
            while (que_2_.size() > 1) {
                que_1_.push_back(que_2_.front());
                que_2_.pop_front();
            }
            ret = que_2_.front();
            que_1_.push_back(ret);
            que_2_.pop_front();
        }
        return ret;
    }

    // Return whether the stack is empty.
    bool empty() {
        return que_1_.empty() && que_2_.empty();
    }
private:
    // toggle queues
    std::deque<int> que_1_;
    std::deque<int> que_2_;
};

implement stack using queues Python

class Stack:
    # initialize your data structure here.
    def __init__(self):
        self.que = []

    # @param x, an integer
    # @return nothing
    def push(self, x):
        self.que.append(x)

    # @return nothing
    def pop(self):
        assert not self.empty(), "empty!"
        self.que.pop()

    # @return an integer
    def top(self):
        assert not self.empty(), "empty!"
        #self.que.reverse()
        ret = self.que[-1]
        #self.que.reverse()
        return ret

    # @return an boolean
    def empty(self):
        return not self.que


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Use linked list to create queue stack BST in C

Use linked list to create queue stack BST in C

c++stack_和_queue用法

  • 2011-08-12 16:47
  • 99KB
  • 下载

Implement Stack using Queues && Implement Queue using Stacks (LeetCode)

Implement Stack using Queues 即使用队列来实现栈 方法:创建两个队列,始终保持一个队列为空。添加数据时,添加到空队列,然后将另一个队列加入到该队列中,这样就保证了最后添加的...

leetcode stack 232. Implement Queue using Stacks

法一:使用两个栈模拟队列。class Queue { private: stack q_input; stack q_output; public: // Push eleme...

【java笔试系列四】Java实现栈Stack和队列Queue

目录(1)栈的创建(2)队列的创建(3)两个栈实现一个队列(4)两个队列实现一个栈(5)设计含最小函数min()的栈,要求min、push、pop、的时间复杂度都是O(1)(6)判断栈的push和po...

c++stack_和_queue用法

  • 2011-05-01 15:56
  • 1.08MB
  • 下载

java基础巩固系列(九):持有对象之间的使用与关系(Iterable、Collection、List、Queue、Set、Map、Stack)

java中的持有对象之间的继承关系

Chapter 9.容器适配器stack、queue、priority_queue

容器适配器 简单的讲就是在已有的顺序容器上面实现的接口,可以方便的使原有的顺序容器有一个新的数据结构 适配器没有提供迭代器,也不能同时插入或删除多个元素 简介 stack        栈...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)