【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 ...
随机推荐
- JS--我发现,原来你是这样的JS:面向对象编程OOP[1]--(理解对象和对象属性类型)
一.介绍 老铁们,这次是JS的面向对象的编程OOP(虽然我没有对象,心累啊,但是可以自己创建啊,哈哈). JS高程里第六章的内容,这章内容在我看来是JS中很难理解的一部分.所以分成三篇博客来逐个理清. ...
- [VUE ERROR] Invalid default value for prop "slides": Props with type Object/Array must use a factory function to return the default value
错误原因: 当给子组件设置 props 属性时,如果参数类型是 Array 或 Object ,它的默认值必须是由工场函数返回,不能直接赋值 错误代码: proE: { type: Array, de ...
- 机器学习是万能的吗?AI落地有哪些先决条件?
机器学习是万能的吗?AI落地有哪些先决条件? https://mp.weixin.qq.com/s/9rNY2YA3BMpoY8NQ_rVIjQ 1.引言 入门机器学习或从事其相关工作前,不知道你思考 ...
- loadrunner 运行场景-常见Graph简介
运行场景-常见Graph简介 by:授客 QQ:1033553122 A. Web Resource Graphs 1. 概述 a) Hits per Second Graph Hits ...
- 第三方apk内置因签名导致SystemUI未启动启动问题案例分析
这个问题是刷完机正常开机后,发现手机无状态栏,下拉通知栏,按音量键也无法出现VolumeDialog,开始看到这个现象感觉是systemUI未编译到版本中去?或者是在systemserver中syst ...
- python包中__init__.py的作用
1.__init__.py定义包的属性和方法 一般为空文件,但是必须存在,没有__init__.py表明他所在的目录只是目录不是包 2.导入包的时候使用 例如有一个test目录,test下有xx1.p ...
- Navicat连接Oracle的几个问题及解决方案
1.用Navicat连接Oracle数据库时报错ORA-28547:connection to server failed,probable Oracle Net admin error 解决方案: ...
- 【PAT】B1039 到底买不买(20)(20 分)
/* 琢磨了很久,当时还没做几道题,参考了柳婼的思路 */ #include<stdio.h> #include<string.h> char arr[1000]={'\0'} ...
- Hadoop2.7.6_04_HDFS的Shell操作与常见问题
1. HDFS的shell操作 1.1. 支持的命令及参数 [yun@mini05 zhangliang]$ hadoop fs Usage: hadoop fs [generic options] ...
- Python作业第一课
零基础开始学习,最近周边的同学们都在学习,我也来试试,嘿嘿,都写下来,下次不记得了还能来看看~~ Python作业第一课1)登陆,三次输入锁定,下次不允许登陆2)设计一个三级菜单,菜单内容可自行定义, ...