Implement the following operations of a queue using stacks.

  • push(x) -- Push element x to the back of queue.
  • pop() -- Removes the element from in front of queue.
  • peek() -- Get the front element.
  • empty() -- Return whether the queue is empty.

Example:

MyQueue queue = new MyQueue();

queue.push(1);
queue.push(2);
queue.peek(); // returns 1
queue.pop(); // returns 1
queue.empty(); // returns false

Notes:

  • You must use only standard operations of a stack -- which means only push to toppeek/pop from topsize, and is empty operations are valid.
  • Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.
  • You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).

这个题目可以用两个stack, 基本思路是如@elmirap提出来的Solution, 两种方式, 第一种是O(n) push, 其他都是O(1)

Code:

class MyQueue(object):
##Solution
# two stacks, O(n) push operation def __init__(self):
"""
Initialize your data structure here.
"""
self.stack = []
self.backup = [] def push(self, x):
"""
Push element x to the back of queue.
:type x: int
:rtype: void
"""
while self.stack:
self.backup.append(self.stack.pop())
self.stack.append(x)
while self.backup:
self.stack.append(self.backup.pop()) def pop(self):
"""
Removes the element from in front of queue and returns that element.
:rtype: int
"""
return self.stack.pop() def peek(self):
"""
Get the front element.
:rtype: int
"""
return self.stack[-1] def empty(self):
"""
Returns whether the queue is empty.
:rtype: bool
"""
return not self.stack # Your MyQueue object will be instantiated and called as such:
# obj = MyQueue()
# obj.push(x)
# param_2 = obj.pop()
# param_3 = obj.peek()
# param_4 = obj.empty()

第二种方式, 平均pop为O(1), 其他都为O(1), 思路实际上类似, 但是在Stack2不急着将其搬回stack1, 因为在stack1搬到stack2之后已经将最后的放在最下面了.所以节约了时间.

Code:

class MyQueue:

    def __init__(self):
"""
Initialize your data structure here.
"""
self.stack = []
self.queue = [] def push(self, x):
"""
Push element x to the back of queue.
:type x: int
:rtype: void
"""
self.stack.append(x) def pop(self):
"""
Removes the element from in front of queue and returns that element.
:rtype: int
"""
if not self.queue:
while self.stack:
self.queue.append(self.stack.pop())
return self.queue.pop() def peek(self):
"""
Get the front element.
:rtype: int
"""
return self.queue[-1] if self.queue else self.stack[0] def empty(self):
"""
Returns whether the queue is empty.
:rtype: bool
"""
return not self.stack and not self.queue # Your MyQueue object will be instantiated and called as such:
# obj = MyQueue()
# obj.push(x)
# param_2 = obj.pop()
# param_3 = obj.peek()
# param_4 = obj.empty()

[LeetCode] 232. Implement Queue using Stacks_Easy tag: Design的更多相关文章

  1. Leetcode 232 Implement Queue using Stacks 和 231 Power of Two

    1. 232 Implement Queue using Stacks 1.1 问题描写叙述 使用栈模拟实现队列.模拟实现例如以下操作: push(x). 将元素x放入队尾. pop(). 移除队首元 ...

  2. [LeetCode] 232. Implement Queue using Stacks 用栈来实现队列

    Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...

  3. (easy)LeetCode 232.Implement Queue using Stacks

    Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...

  4. Java [Leetcode 232]Implement Queue using Stacks

    题目描述: Implement the following operations of a queue using stacks. push(x) -- Push element x to the b ...

  5. LeetCode 232 Implement Queue using Stacks

    Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...

  6. [LeetCode] 225. Implement Stack using Queues_Easy tag: Design

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

  7. Java for LeetCode 232 Implement Queue using Stacks

    Stack<Integer> stack=new Stack<Integer>(); public void push(int x) { stack.push(x); } // ...

  8. Leetcode 232 Implement Queue using Stacks STL

    本题用两个栈实现队列,用栈的基本操作去实现队列的所有基本操作push(),pop(),peek()以及empty() sa作为输入栈,sb作为输出栈,将sa输入元素的反转过来放到sb中 push与sa ...

  9. LeetCode 232 Implement Queue using Stacks 两个栈实现队列

    class MyQueue { public: /** Initialize your data structure here. */ MyQueue() { } /** Push element x ...

随机推荐

  1. python基础---->python的使用(七)

    这里记录python关于io.装饰器和序列化的一些知识.面对大河我无限惭愧,我年华虚度,空有一身疲倦,和所有以梦为马的诗人一样,岁月易逝 一滴不剩. python的一些知识 一.python中的装饰器 ...

  2. Makefile中的MAKECMDGOALS

      make 在执行时会设置一个特殊变量 -- "MAKECMDGOALS" ,该变量记录了命令行参数指定的终极目标列表,没有通过参数指定终极目标时此变量为空.该变量仅限于用在特殊 ...

  3. 【Spring Boot && Spring Cloud系列】那些Spring Boot中踩过的坑

    一.不连接数据库启动springboot报错 Cannot determine embedded database driver class for database type NONE 原因:Spr ...

  4. C程序设计语言习题(3-3)

    编写函数expand(s1,s2), 将字符串s1中类似于a-z一类的速记符号在字符串s2中扩展为等价的完整列表abc……xyz.该函数可以处理大小写字母和数字,并可以处理a-b-c.a-z0-9与a ...

  5. 使用iLO远程管理HP系列服务器

    iLO是Integrated Ligths-out的简称,是HP服务器上集成的远程管理端口,它是一组芯片内部集成vxworks嵌入式操作系统,通过一个标准RJ45接口连接到工作环境的交换机.只要将服务 ...

  6. 【CF889E】Mod Mod Mod DP

    [CF889E]Mod Mod Mod 题意:给你一个序列$a_1,a_2...a_n$,定义$f(x,n)=x\mod a_n$,$f(x,i)=x\mod a_i+f(x \mod a_i,i+1 ...

  7. Unity笔记 英保通 Unity新的动画系统Mecanim

    Mecanim动画系统是Unity独一无二.强大灵活的人物动画系统.该系统赋予您的人类和非人类人物令人难以置信的自然流畅的动作,使它们栩栩如生.游戏中角色设计提高到了新的层次,在处理人类动画角色中可以 ...

  8. 移动端mobiscroll无法滑动、无法划动选值的问题

    mobiscroll配置 theme: 'ios',时.滑动取值无效: html的页面内容稍微长过手机屏幕,页面无法完全加载,允许稍微滑动,这时导致点击选择mobiscroll值时无法滑动取值.处理: ...

  9. iOS - 利用 iTunes 接口检查 App 版本更新

    iOS 想要检查 App 当前版本是否为最新,一般的方案大概都是服务器自己提供一个接口来获取 App 最新版本是多少,然后再做出相应提示是否需要更新,但是接口需要手动维护,应用要审核,还得等审核通过以 ...

  10. Java基础之理解封装,继承,多态三大特性

    目录 封装 继承 多态 封装 封装隐藏了类的内部实现机制,可以在不影响使用的情况下改变类的内部结构,同时也保护了数据.对外界而已它的内部细节是隐藏的,暴露给外界的只是它的访问方法. 代码理解 publ ...