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.

代码如下:

class MyQueue {
// Push element x to the back of queue.
Stack<Integer>stack=new Stack<Integer>();
public void push(int x) {
if(stack.empty())
stack.push(x);
else{
Stack<Integer> ss=new Stack<Integer>();
while(!stack.empty())
{
ss.push(stack.peek());
stack.pop();
}
stack.push(x);
while(!ss.empty())
{
stack.push(ss.peek());
ss.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.empty();
}
}

232. Implement Queue using Stacks的更多相关文章

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

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

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

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

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

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

  5. Java [Leetcode 232]Implement Queue using Stacks

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

  6. LeetCode 232 Implement Queue using Stacks

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

  7. 【LeetCode】232. Implement Queue using Stacks

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

  8. 【一天一道LeetCode】#232. Implement Queue using Stacks

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Impleme ...

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

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

随机推荐

  1. linux之开发板与宿主机-GDB远程调试

    平台: redhat9.0 下载 gdb-5.3.tar.gz 解压 gdb-5.3.tar.gz: #tar vzxf gdb-5.3.tar.gz 配置文件# ./configure - targ ...

  2. initWithFrame 和 initWithCoder

    当我们所写的程序里没用用Nib文件(XIB)时,用代码控制视图内容,需要调用initWithFrame去初始化 - (id)initWithFrame:(CGRect)frame { if (self ...

  3. C++学习 之const

    const在C++中很常用,在编程中也建议多使用const去告诉编译器和其他程序员某个值应该保持不变. const可以用在很多地方: (1)用在classes外部修饰global或namespace作 ...

  4. IOS 导航栏属性设置

    IOS 7 以上系统导航栏: [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]]; // 返回按钮颜色 [UINaviga ...

  5. arguments.callee

    arguments.callee在哪个函数中运行,他就代表哪个函数,一般在匿名函数中.在匿名函数中有时需要自己调用自己,但是由于是匿名函数,没有名字,所以可以用arguments.callee来代替匿 ...

  6. Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

    Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK fla ...

  7. 2016 - 1 -17 GCD学习总结

    一:GCD中的两个核心概念,队列与任务: 1.任务:执行什么操作.(代码块 block) 任务执行的类型分为以下两种: 1.1同步执行任务:在当前线程执行任务.不会开辟新的线程. 1.2异步执行任务: ...

  8. 在某个目录下的所有文件中查找包含某个字符串的Windows命令

    findstr可以完成这个工作.   上面的命令表示,当前目录以及当前目录的所有子目录下的所有文件中查找"string"这个字符串. *.*表示所有类型的文件. /s 表示当前目录 ...

  9. Linux内核中关于内存的数据结构

    物理页面 /* * Try to keep the most commonly accessed fields in single cache lines * here (16 bytes or gr ...

  10. BZOJ 2200 道路与航线

    好厉害呀这道题,有种豁然开朗的感觉.... 按拓扑顺序跑最短路. 然后注意细节,像WA的代码犯下的错是一笔带过没有丝毫考虑的...然而就是错了. 考试的时候一定要拍啊. #include<ios ...