【LeetCode225】 Implement Stack using Queues★
1.题目

2.思路


3.java代码
import java.util.LinkedList;
import java.util.Queue; public class MyStack {
private Queue<Integer> q1=new LinkedList<Integer>();
private Queue<Integer> q2=new LinkedList<Integer>(); /** Push element x onto stack. */
public void push(int x) {
if(!q1.isEmpty())
q1.add(x);
else
q2.add(x);
} /** Removes the element on top of the stack and returns that element. */
public int pop() {
if(q1.isEmpty()){
int size=q2.size();
for(int i=1;i<size;i++){
q1.add(q2.poll());
}
return q2.poll();
}else{
int size=q1.size();
for(int i=1;i<size;i++){
q2.add(q1.poll());
}
return q1.poll();
}
} /** Get the top element. */
public int top() {
int result;
if(q1.isEmpty()){
int size=q2.size();
for(int i=1;i<size;i++){
q1.add(q2.poll());
}
result=q2.poll();
q1.add(result);
}else{
int size=q1.size();
for(int i=1;i<size;i++){
q2.add(q1.poll());
}
result=q1.poll();
q2.add(result);
}
return result;
} /** Returns whether the stack is empty. */
public boolean empty() {
return q1.isEmpty()&&q2.isEmpty();
}
} /**
* Your MyStack object will be instantiated and called as such:
* MyStack obj = new MyStack();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.top();
* boolean param_4 = obj.empty();
*/
【LeetCode225】 Implement Stack using Queues★的更多相关文章
- 【LeetCode 225_数据结构_栈_实现】Implement Stack using Queues
class Stack { public: // Push element x onto stack. void push(int x) { int len = nums.size(); nums.p ...
- 【LeetCode】栈 stack(共40题)
[20]Valid Parentheses (2018年11月28日,复习, ko) 给了一个字符串判断是不是合法的括号配对. 题解:直接stack class Solution { public: ...
- 【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( ...
- leetcode:Implement Stack using Queues 与 Implement Queue using Stacks
一.Implement Stack using Queues Implement the following operations of a stack using queues. push(x) - ...
- 232. Implement Queue using Stacks,225. Implement Stack using Queues
232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...
- 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 ...
- Implement Queue by Two Stacks & Implement Stack using Queues
Implement Queue by Two Stacks Implement the following operations of a queue using stacks. push(x) -- ...
- 【LeetCode】225. Implement Stack using Queues
题目: Implement the following operations of a stack using queues. push(x) -- Push element x onto stack ...
- 【一天一道LeetCode】#225. Implement Stack using Queues
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Impleme ...
随机推荐
- C++知识回顾之__stdcall、__cdcel和__fastcall三者的区别
__stdcall.__cdecl和__fastcall是三种函数调用协议,函数调用协议会影响函数参数的入栈方式.栈内数据的清除方式.编译器函数名的修饰规则等. 调用协议常用场合 __stdcall: ...
- 基于Grafana的监控数据钻取功能应用实践
互联网企业中,随着机器规模以及业务量的爆发式增长,监控数据逐渐成为一种大数据,对监控大数据的分析,包括数据采集.数据缓存.数据聚合分析.数据存储.数据展现等几个阶段.不同阶段有不同的解决方案及支撑工具 ...
- JMeter 利用Jmeter批量数据库插入数据
利用Jmeter批量数据库插入数据 by:授客 QQ:1033553122 1. 启动Jmeter 2. 添加 DBC Connection Configuration 右键线程组-> ...
- Clumsy 利用无线网卡结合Clumsy软件模拟弱网络测试
利用无线网卡结合Clumsy软件模拟弱网络测试 by:授客 QQ:1033553122 实践环境 Clumsy 0.2 下载地址:http://jagt.github.io/clumsy/downlo ...
- LDA背景资料
[https://zhuanlan.zhihu.com/p/30226687] LDA模型的前世今生 在文本挖掘中,有一项重要的工作就是分析和挖掘出文本中隐含的结构信息,而不依赖任何提前标注的信息.L ...
- 服务器 nginx配置 防止其他域名绑定自己的服务器
基于我的网站被其他的域名恶意绑定了,我做出了如下处理,全站转https,同时配置nginx跳转禁止其他绑定ip的域名访问(原理主机空域名可绑定任意的,参考https://www.jb51.net/ar ...
- webApi core2 DI通过代码来获取容器里面已注入的对象
请求服务 来自 HttpContext 的一次 ASP.NET 请求中可用的服务通过 RequestServices 集合公开的. 请求服务将你配置的服务和请求描述为应用程序的一部分.当你的对象指定依 ...
- shell 的条件表达式及逻辑操作符简单介绍
查看系统的shell: cat /etc/shells 文件测试表达式: -f 文件 文件存在且为普通文件则真,即测试表达式成立. -d 文件 文件存在且为目录文件则真,即测试表达式成立. -s ...
- 关掉 ubuntu bug 报告功能
想关掉这个: 通过服务加配置文件关掉
- MySQL使用索引的场景分析、不能使用索引的场景分析
一.MySQL中能够使用索引的典型场景 1.匹配全值.对索引中的列都有等值匹配的条件.即使是在and中,and前后的列都有索引并进行等值匹配. 2.匹配值的范围查询,对索引的值能够进行范围查找. 3. ...