Coursera Algorithms week2 栈和队列 Interview Questions: Queue with two stacks

题目原文:

Implement a queue with two stacks so that each queue operations takes a constant amortized number of stack operations.

题目要求用栈实现队列的所有操作。

 1 import java.util.Iterator;
 2 import edu.princeton.cs.algs4.Stack;
 3 public class QueueWith2Stacks<Item>{
 4     Stack<Item> inStack = new Stack<Item>();
 5     Stack<Item> outStack = new Stack<Item>();
 6     public boolean isEmpty(){
 7         if(inStack.isEmpty() && outStack.isEmpty())
 8             return true;
 9         else return false;
10     }
11     public int size(){
12         return (inStack.size() + outStack.size());
13     }
14     public void enqueue(Item item){
15         inStack.push(item);
16     }
17     public Item dequeue(){
18         if(outStack.isEmpty()){
19             if(inStack.isEmpty()) return null;
20             else{
21                 Iterator<Item> sIter = inStack.iterator();
22                 while(sIter.hasNext()){
23                     outStack.push(sIter.next());
24                 }
25                 return outStack.pop();
26             }
27         }else{
28             return outStack.pop();
29         }
30     }
31 }
时间: 2024-12-28 18:53:09

Coursera Algorithms week2 栈和队列 Interview Questions: Queue with two stacks的相关文章

Coursera Algorithms week2 栈和队列 练习测验: Stack with max

题目原文: Stack with max. Create a data structure that efficiently supports the stack operations (push and pop) and also a return-the-maximum operation. Assume the elements are reals numbers so that you can compare them. 分析: 该题目要求在实现正常stack的push和pop操作外,还

LeetCode 232题用栈实现队列(Implement Queue using Stacks) Java语言求解

题目链接 https://leetcode-cn.com/problems/implement-queue-using-stacks/ 题目描述 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部.pop() -- 从队列首部移除元素.peek() -- 返回队列首部的元素.empty() -- 返回队列是否为空. 示例: MyQueue queue = new MyQueue();queue.push(1);queue.push(2); queue.peek(); //

Coursera Algorithms Programming Assignment 2: Deque and Randomized Queue

这次作业与第一周作业相比,稍微简单一些.有三个编程练习:双端队列(Deque)设计.随机队列(Randomized Queue)设计,还有一个排列组合类Permutation. 一.双端队列Deque 设计要求:A double-ended queue or deque (pronounced "deck") is a generalization of a stack and a queue that supports adding and removing items from e

Coursera Algorithms week2 基础排序 Interview Questions: 2 Permutation

题目原文: Given two integer arrays of size n , design a subquadratic algorithm to determine whether one is a permutation of the other. That is, do they contain exactly the same entries but, possibly, in a different order. 本质上就是求两个数组排序后是否相等,鉴于本节课学的是选择.插入.

Coursera Algorithms week2 基础排序 Interview Questions: 1 Intersection of two sets

题目原文: Given two arrays a[] and b[], each containing n distinct 2D points in the plane, design a subquadratic algorithm to count the number of points that are contained both in array a[] and array b[]. 题目的目标就是计算重复point的个数,很简单,代码如下 1 import java.awt.Po

Java之用栈实现队列

队列是一种典型的先进先出数据结构,队列的实现方式有很多种,比如数组,比如链表等,队列也可以用两个栈来实现,下面就用两个栈实现一个队列. 原理 两个栈中,一个栈用来入队,叫他入队栈,另一个栈用来出队,叫出队栈. 当入队时,我们直接把元素压入入队栈. 当出队时,分两种情况:   ①若出队栈不为空,则直接出队栈栈顶元素出栈   ②若出队栈为空,则入队栈元素依次出栈并压入出队栈,然后出队栈出栈 具体实现 知道了原理后,实现起来就很简单啦,这里我们借助JDK中的java.util.stack类来作为我们队

Leetcode题解——数据结构之栈和队列

1. 用栈实现队列 2. 用队列实现栈 3. 最小值栈 4. 用栈实现括号匹配 5. 数组中元素与下一个比它大的元素之间的距离 6. 循环数组中比当前元素大的下一个元素 1. 用栈实现队列 232. Implement Queue using Stacks (Easy) 栈的顺序为后进先出,而队列的顺序为先进先出.使用两个栈实现队列,一个元素需要经过两个栈才能出队列,在经过第一个栈时元素顺序被反转,经过第二个栈时再次被反转,此时就是先进先出顺序. class MyQueue { private

安卓面试题 Android interview questions

安卓面试题 Android interview questions 作者:韩梦飞沙 ?2017?年?7?月?3?日,??14:52:44 1.      要做一个尽可能流畅的ListView,你平时在工作中如何进行优化的? ①Item布局,层级越少越好,使用hierarchyview工具查看优化. ②复用convertView ③使用ViewHolder ④item中有图片时,异步加载 ⑤快速滑动时,不加载图片 ⑥item中有图片时,应对图片进行适当压缩 ⑦实现数据的分页加载 2.      对

浅谈算法和数据结构(1):栈和队列

浅谈算法和数据结构(1):栈和队列 2014/11/03 ·  IT技术                                         · 2 评论                                      ·  数据结构, 栈, 算法, 队列 分享到: 60 SegmentFault D-Day 2015 北京:iOS 站 JDBC之“对岸的女孩走过来” CSS深入理解之relative HTML5+CSS3实现春节贺卡 原文出处: 寒江独钓   欢迎分享原创