leetcode系列(9)miniStack,stack_to_queue, queue_to_stack
来源:互联网 发布:网络经常被攻击 编辑:程序博客网 时间:2024/06/05 20:57
由于种种原因没有做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
- leetcode系列(9)miniStack,stack_to_queue, queue_to_stack
- MiniStack(找栈中最小元素的栈)
- Leetcode--easy系列9
- leetCode系列(一):计数
- 【LeetCode】字符串系列(匹配)
- 【LeetCode】字符串系列(其他)
- 【LeetCode】字符串系列(Anagram)
- 【LeetCode】数组系列(间隔)
- 【LeetCode】数组系列(排序)
- Leetcode 题解系列(一)
- Leetcode 题解系列(二)
- Leetcode 题解系列(三)
- Leetcode 题解系列(四)
- Leetcode 题解系列(五)
- Leetcode 题解系列(六)
- Leetcode 题解系列(七)
- Leetcode 题解系列(八)
- Leetcode 题解系列(九)
- Haskell趣学指南学习日记(1)
- SecureCRT 连接sqlplus删除键失效解决方法
- 带下拉刷新的瀑布流 不会oom
- 2015湖南省队集训DAY4——hoodle
- 《Opencv 入门教程》
- leetcode系列(9)miniStack,stack_to_queue, queue_to_stack
- 大阪中文离线地图App上线
- OpenGL学习脚印: 投影矩阵的推导
- HDU 1074 Doing Homework (状压DP + 路径记录)
- 一.Spring学习之旅----初识Spring
- 文章标题
- 简单的模拟多线程引用计数原理
- cin.clear()、cin.sync()和cin.ignor()的用法
- 《opencv入门教程》