题目:

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 top
    peek/pop from topsize,
    and is emptyoperations 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> stack=new Stack<>();
Stack<Integer> stack2=new Stack<>(); public void push(int x) {
while(!stack.isEmpty())
{
stack2.push(stack.pop());
}
stack2.push(x);
while(!stack2.isEmpty())
{
stack.push(stack2.pop());
} } // Removes the element from in front of queue.
public void pop() {
stack.pop();
} // Get the front element.
public int peek() {
return stack.peek();
} // Return whether the queue is empty.
public boolean empty() {
return stack.isEmpty();
}
}

在出栈的时候进行处理:

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

LeetCode232 Implement Queue using Stacks Java 题解的更多相关文章

  1. LeetCode232:Implement Queue using Stacks

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

  2. Lintcode: Implement Queue by Stacks 解题报告

    Implement Queue by Stacks 原题链接 : http://lintcode.com/zh-cn/problem/implement-queue-by-stacks/# As th ...

  3. LeetCode 232. 用栈实现队列(Implement Queue using Stacks) 4

    232. 用栈实现队列 232. Implement Queue using Stacks 题目描述 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从 ...

  4. leetcode:Implement Stack using Queues 与 Implement Queue using Stacks

    一.Implement Stack using Queues Implement the following operations of a stack using queues. push(x) - ...

  5. 【LeetCode】232 & 225 - Implement Queue using Stacks & Implement Stack using Queues

    232 - Implement Queue using Stacks Implement the following operations of a queue using stacks. push( ...

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

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

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

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

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

  9. 【LeetCode】232. Implement Queue using Stacks 解题报告(Python & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Python解法 Java解法 日期 [LeetCo ...

随机推荐

  1. js php 互调

    1 JS方式调用PHP文件并取得php中的值 举一个简单的例子来说明: 如在页面a.html中用下面这句调用: <! DOCTYPE HTML><html><head&g ...

  2. pycharm下多个工程项目并存显示

    问题:使用pycharm新建一个工程时,出现如下提示: 无论选择哪一个,都会发现之前已经建立的工程没有并存显示 解决办法: 1. 找到file->settings: 2.点击project st ...

  3. OSPF 一 基础

    本节介绍ospf路由选择协议    为链路状态  路由选择协议 一  分类 open shortest path first   开放最短路优先   公有协议 单区域的ospf实施  运行在一个自治系 ...

  4. xtu数据结构 B. Get Many Persimmon Trees

    B. Get Many Persimmon Trees Time Limit: 1000ms Memory Limit: 30000KB 64-bit integer IO format: %lld  ...

  5. Centos6.5搭建git远程仓库

    远程仓库搭建 step1:安装git ```yum -y install git``` step2:创建用户git,用来运行git服务 useradd git passwd git //修改git用户 ...

  6. 第一阶段Sprint 对其他团队评价

    咱们的team 针对对“小学生网页四则运算”这个产品的评审,本人提出建议:1.第一阶段的产品Sprint不够好,无任务看板.无燃尽图.希望完善该产品的的Sprint,第二阶段的Sprint要认真写好. ...

  7. Windows Server AppFabric

    文章:Windows Server AppFabric简介 介绍了AppFabric强大的功能.

  8. redis中redis.conf配置文件

    redis.conf文件配置解释 1. Redis默认不是以守护进程的方式运行,可以通过该配置项修改,使用yes启用守护进程 daemonize yes 2. 当Redis以守护进程方式运行时,Red ...

  9. Mountaineers

    Mountaineers 时间限制: 3 Sec  内存限制: 128 MB 题目描述 The Chilean Andes have become increasingly popular as a ...

  10. HDU 4022 stl multiset

    orz kss太腻害了. 一.set和multiset基础 set和multiset会根据特定的排序准则,自动将元素进行排序.不同的是后者允许元素重复而前者不允许. 需要包含头文件: #include ...