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.

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).

代码如下:

class MyQueue {
// Push element x to the back of queue.
Stack<Integer> stack1=new Stack<Integer>();
Stack<Integer> stack2=new Stack<Integer>();
public void push(int x) {
stack1.push(x);
} // Removes the element from in front of queue.
public void pop() {
while(stack1.size()>1) stack2.push(stack1.pop());
stack1.pop();
while(stack2.size()>0) stack1.push(stack2.pop());
} // Get the front element.
public int peek() {
while(stack1.size()>1) stack2.push(stack1.pop());
int x= stack1.peek();
while(stack2.size()>0) stack1.push(stack2.pop());
return x;
} // Return whether the queue is empty.
public boolean empty() {
return stack1.empty();
}
}

  运行结果:

(easy)LeetCode 232.Implement Queue using Stacks的更多相关文章

  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. Java [Leetcode 232]Implement Queue using Stacks

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

  4. LeetCode 232 Implement Queue using Stacks

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

  5. Java for LeetCode 232 Implement Queue using Stacks

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

  6. Leetcode 232 Implement Queue using Stacks STL

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

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

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

  8. leetcode 155. Min Stack 、232. Implement Queue using Stacks 、225. Implement Stack using Queues

    155. Min Stack class MinStack { public: /** initialize your data structure here. */ MinStack() { } v ...

  9. 232. Implement Queue using Stacks,225. Implement Stack using Queues

    232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...

随机推荐

  1. each和$(this)配合循环_siblings选取同级不同类型元素

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...

  2. Android 6.0删除Apache HttpClient相关类的解决方法

    相应的官方文档如下: 上面文档的大致意思是,在Android 6.0(API 23)中,Google已经移除了Apache HttpClient相关的类,推荐使用HttpUrlConnection. ...

  3. javascript 中根据sort 方法随机数组 (Math.random)

    var arr = [1,2,3,4,5,6,7,8,9,10]; function Arandom(a,b){ return (Math.random() > 0.5) ? 1 : -1;; ...

  4. UIview定义背景图片

    UIImage *image = [UIImage imageNamed:@"bgimagename"];    UIView *view = [[UIView alloc]ini ...

  5. HackerRank "Bike Racer"

    Just for study from its editorial~ Lesson learnt: an optimized Hungarian Algorithm: Hopcroft-Karp Al ...

  6. Python基础教程【读书笔记】 - 2016/7/7

    希望通过博客园持续的更新,分享和记录Python基础知识到高级应用的点点滴滴! 第四波:第5章  条件.循环和其他语句 [总览]  深入介绍条件语句和循环语句,随后会看到列表推导式如何扮演循环和条件语 ...

  7. bzoj3521: [Poi2014]Salad Bar

    Description 有一个长度为n的字符串,每一位只会是p或j.你需要取出一个子串S(从左到右或从右到左一个一个取出),使得不管是从左往右还是从右往左取,都保证每时每刻已取出的p的个数不小于j的个 ...

  8. php测试程序运行时间和占用内存情况

    php测试程序运行时间和占用内存情况: $HeaderTime = microtime(true);//参数true表示返回浮点数值 /** *CODE */ printf(" total ...

  9. Env:ctags和Taglist安装与配置

    注:文章参照http://blog.csdn.net/vaqeteart/article/details/4146618 想必用过Source Insight的人都记得这样一个功能: SI能够把当前文 ...

  10. GTD_百度百科

    GTD就是Getting Things Done的缩写,翻译过来就是"把事情做完",GTD的核心理念概括就是必须记录下来要做的事,然后整理安排并自己一一去执行.GTD的五个核心原则 ...