Description:

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 top, size, 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).

用栈来实现队列的功能。类似的还有用队列实现栈的功能。

思路:栈和队列对数据的处理是不同的。栈是先进后出(FILO)队列是先进先出(FIFO)。所以要用两个栈来维护一个队列。一个数据栈,一个暂存数据栈。数据栈用来存储数据,暂存数据栈用来把数据栈中的数据首尾交换,模拟队列的数据操作。

代码:

class MyQueue {
Stack<Integer> stack1;
Stack<Integer> stack2; public MyQueue() {
stack1 = new Stack();
stack2 = new Stack();
} // Push element x to the back of queue.
public void push(int x) {
stack1.push(x);
} // Removes the element from in front of queue.
public void pop() {
//把栈中的元素移到另一个栈中,首尾倒序。
while(!stack1.empty()) {
stack2.push(stack1.pop());
}
//移除堆首元素。
stack2.pop();
//还原数据队列。
while(!stack2.empty()) {
stack1.push(stack2.pop());
}
} // Get the front element.
public int peek() {
//把栈中的元素移到另一个栈中,首尾倒序。
while(!stack1.empty()) {
stack2.push(stack1.pop());
}
//获取堆首元素。
int front = stack2.peek();
//还原数据队列。
while(!stack2.empty()) {
stack1.push(stack2.pop());
}
return front;
} // Return whether the queue is empty.
public boolean empty() {
return stack1.empty();
}
}

LeetCode——Implement Queue using Stacks的更多相关文章

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

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

  2. Leetcode Implement Queue using Stacks

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

  3. LeetCode Implement Queue using Stacks (数据结构)

    题意: 用栈来实现队列. 思路: 一个栈是不够的,至少要两个. (1)插入.永远只插入到stack1中(插到栈顶). (2)弹出.如果stack2不为空,直接弹出stack2的栈顶,否则,将stack ...

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

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

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

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

  6. 【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( ...

  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. 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. 小任务tasklet应用

    一个使用tasklet的中断程序首先会通过执行中断处理程序来快速完成上半部分的工作,接着通过调度tasklet使得下半部分的工作得以完成,但是下半部分何时执行属于内核的工作. 由于tasklet依靠软 ...

  2. BusyBox inittab

    # /etc/inittab init(8) configuration for BusyBox## Copyright (C) 1999-2004 by Erik Andersen <ande ...

  3. Spring引入配置文件

    1.spring.xml加载映射的配置配置文件 <!--采用这种方式简化配置文件--> <context:property-placeholder location="cl ...

  4. C语言实现商品销售系统

    商品销售系统 #include<stdio.h> //头文件 #include<string.h> //头文件 #include<stdlib.h> //头文件 # ...

  5. 在linux上安装redmine

    Redmine 是一个开源的.基于Web的项目管理和缺陷跟踪工具.它用日历和甘特图辅助项目及进度可视化显示.同时它又支持多项目管理.Redmine是一个自由开放 源码软件解决方案,它提供集成的项目管理 ...

  6. 内网环境NTP服务及时间同步(CentOS6.x)配置和部署

    目标环境,5台linux centos 6.3, 一台作为NTPD服务与外部公共NTP服务同步时间,同时作为内网的NTPD服务器,其他机器与这台服务做时间同步.  服务器IP 角色   说明 同步方式 ...

  7. iOS边练边学--Segue数据逆传(用block代替delegate)

    一.block与方法的异同点: 相同点是都是保存代码段,什么时候执行,什么时候调用 不同点是block不受类或者对象的约束:方法收到了类或者对象的约束 二.思路:(通讯录练习) 在联系人控制器中,添加 ...

  8. mongodb可以通过profile来监控数据 (mongodb性能优化)

    mongodb可以通过profile来监控数据 (mongodb性能优化)   开启 Profiling  功能 ,对慢查询进行优化: mongodb可以通过profile来监控数据,进行优化. 查看 ...

  9. 统一建模语言 UML

    目录 统一建模语言 UML UML定义了5类10种模型图 一用例图用于建立需求模型 二静态图主要描述系统的静态表示和关系包括类图包图对象图 三行为图描述系统动态模型和对象组成的交换关系包括状态图和活动 ...

  10. int、long、longlong、float、double、long double的范围