【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 ...
随机推荐
- 【代码笔记】Web-HTML-布局
一, 效果图. 二,代码. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...
- Android Studio_更新Gradle
一.Gradle更新问题 Android Studio每次更新版本都会更新Gradle这个插件,而且有时候提示更新,却一直更新不了,那是因为中国伟大的长城问题.就是下图,我刚刚更新了,提示更新grad ...
- spring BeanFactory及ApplicationContext中Bean的生命周期
spring bean 的生命周期 spring BeanFactory及ApplicationContext在读取配置文件后.实例化bean前后.设置bean的属性前后这些点都可以通过实现接口添加我 ...
- python 3.3.2报错:No module named 'urllib2'
ModuleNotFoundError: No module named 'urllib3' 1. ImportError: No module named 'cookielib'1 Python3中 ...
- 浅谈C#依赖注入
什么是依赖注入?不管是js中的一些前端框架还是,java,C#,php等中的一些后端开发框架中,都会涉及这个看着逼格略高的词语:依赖注入,越是看着好像很厉害的东西越是会让许多人学习产生恐惧,好像很厉害 ...
- Centos7查询开机启动项服务
问题描述: 最近安装了zabbix设置了一些开机启动服务 例如:zabbix-server.service,httpd.service,mariadb.service,或者系统的firework.se ...
- VMware导入OVF时报错(未能部署OVF包用户取消了任务的解决办法)
阅读目录: 1.问题 2.原因 3.解决方案 问题:部署OVF模版的时候报错“用户取消了任务” 原因:导出ovf模板时,虚拟CD-ROM的选项要选[客户端设备],否则导入时报错“用户取消了任务” 解决 ...
- VSCode + PYQT5 + QtDesigner 环境搭建和测试
目的:编写Python桌面应用程序. 备注:也可以选择VS2017+QtDesigner ,但更喜欢VSCode 第1步:安装PyQt5和PyQt5-tools pip3 install -i htt ...
- Windows Server 2008 R2终端服务器激活方法
本文描述了如何激活Windows Server 2008 R2的终端服务器的方法. 目录: 1.Windows Server 2008 R2终端服务器的安装 2.Windows Server 20 ...
- Centos7.2中安装pip
CentOS安装python-pip 在使用Python时,需要导入一些第三方工具包,一般情况下,鼓励使用pip来安装管理这些第三方的包, 这里我们来看一下如何在CentOS 7.2上安装Python ...