关闭

leetcode系列(9)miniStack,stack_to_queue, queue_to_stack

标签: c++leetcodepython
435人阅读 评论(0) 收藏 举报
分类:

由于种种原因没有做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


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:49065次
    • 积分:1505
    • 等级:
    • 排名:千里之外
    • 原创:105篇
    • 转载:0篇
    • 译文:0篇
    • 评论:5条
    文章分类
    最新评论