LeetCode(23)-Implement Queue using Stacks
题目:
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).
思路:
- 这道题的意思是用堆栈(stack)来实现队列(quene)
- 可以用两个stack,s1和s2来实现,s1用来整体的存储,s2用来缓冲倒叙,首先判断s2是否为空,为空,把s1弹出到s2,否则s2.pop()
代码:
class MyQueue {
Stack<Integer> s1 = new Stack<>();
Stack<Integer> s2 = new Stack<>();
// Push element x to the back of queue.
public void push(int x) {
s1.push(x);
}
// Removes the element from in front of queue.
public void pop() {
if(!s2.isEmpty()){
s2.pop();
}else{
if(s1.isEmpty()){
return;
}else{
while(!s1.isEmpty()){
int tmp = s1.pop();
s2.push(tmp);
}
s2.pop();
}
}
}
// Get the front element.
public int peek() {
if(!s2.isEmpty()){
return s2.peek();
}else{
if(s1.isEmpty()){
return -1;
}else{
while(!s1.isEmpty()){
int tmp = s1.pop();
s2.push(tmp);
}
return s2.peek();
}
}
}
// Return whether the queue is empty.
public boolean empty() {
return(s1.isEmpty() && s2.isEmpty());
}
}
LeetCode(23)-Implement Queue using Stacks的更多相关文章
- Leetcode 232 Implement Queue using Stacks 和 231 Power of Two
1. 232 Implement Queue using Stacks 1.1 问题描写叙述 使用栈模拟实现队列.模拟实现例如以下操作: push(x). 将元素x放入队尾. pop(). 移除队首元 ...
- [LeetCode] 232. Implement Queue using Stacks 用栈来实现队列
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- (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 ...
- Java [Leetcode 232]Implement Queue using Stacks
题目描述: Implement the following operations of a queue using stacks. push(x) -- Push element x to the b ...
- LeetCode 232 Implement Queue using Stacks
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- leetCode(37):Implement Queue using Stacks
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- Java for LeetCode 232 Implement Queue using Stacks
Stack<Integer> stack=new Stack<Integer>(); public void push(int x) { stack.push(x); } // ...
- Leetcode 232 Implement Queue using Stacks STL
本题用两个栈实现队列,用栈的基本操作去实现队列的所有基本操作push(),pop(),peek()以及empty() sa作为输入栈,sb作为输出栈,将sa输入元素的反转过来放到sb中 push与sa ...
- LeetCode 232 Implement Queue using Stacks 两个栈实现队列
class MyQueue { public: /** Initialize your data structure here. */ MyQueue() { } /** Push element x ...
随机推荐
- Android开发_TextView跑马灯
关键代码: android:singleLine="true" android:ellipsize="marquee" android:focusable=&q ...
- javascript之事件处理
一般事件 onclick 鼠标点击时触发此事件 ondblclick 鼠标双击时触发此事件 onmousedown ...
- Android简易实战教程--第十一话《获取手机所有应用信息Engine类详解》
如果想要获取系统手机应用的详细信息,那么下边代码可以直接作为模板使用.笔者对每一行代码都做了注解,供您参考.直接上代码: package com.example.itydl.engines; impo ...
- 【ShaderToy】开篇
写在前面 呜呼,好久没有写博客了,好惭愧.题外话,感觉越大就越想家,希望可以一直和家人在一起,哪怕只是坐在一起不说话也觉得很温暖,一想到要分开眼睛就开始酸,哎.开学还是爬上来老实更新博客学习吧~ 今天 ...
- Oracle使用游标删除所有用户数据表中的所有记录脚本
应用场景:因为数据库中的数据涉及机密信息,希望一次性能删除掉所有数据,只保留数据表结构,供新项目开发程序用 测试结果:经查询已删除所有数据 存在问题:数据表如果存在外键的话下面脚本可能执行不成功,请自 ...
- Eclipse中如何快速查看jar包中 的class源码
我们查看jar源码时,一般是安装个jd-gui,把jar拷出来,然后从jd-gui中打开jar再查看源码,这个过程不免有些麻烦,当然,本篇所讲的快速查看的方法也没什么高科技手段,只是将jd-gui集成 ...
- (一一五)利用NSKeyedArchiver实现任意对象转为二进制
[应用背景] 在数据库中存储数据时,如果对象过于复杂,又不必要创建复杂的表,可以直接把整个对象转化为二进制存入数据库字段,然后取出后再还原即可. [实现方法] 在PHP中,使用序列化和反序列化可以实现 ...
- Spark技术内幕:一个图搞定Spark到底有多少行代码
Spark1.0.0发布一个多月了,那么它有多少行代码(Line of Code, LOC)? 注:代码统计未包含测试,sample.
- 程序员大牛 Jeff Atwood 的两本中文书
程序员大牛,StackOverflow.com创始人之一--Jeff Atwood 英文博客:http://blog.codinghorror.com <高效能程序员的修炼>,人民邮电出版 ...
- 【Android 应用开发】 Android APK 反编译 混淆 反编译后重编译
反编译工具 : 总结了一下 linux, windows, mac 上的版本, 一起放到 CSDN 上下载; -- CSDN 下载地址 : http://download.csdn.net/detai ...