【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-ionic-ionicLab的使用
一,下载地址为:http://lab.ionic.io/,下载后直接安装即可.图标如下图所示: 二,打开ionicLab,如下图所示: 三,打开一个已有的文件,File-->Open Exist ...
- 【读书笔记】iOS-设计简单的Frenzic式益智游戏
如果你决定用UIView动画或Core Animation,一定要编写一些测试用例,模拟游戏可能遇到的要求最高的动画,另外不要忘记播放声音.不要等到最后才增加声音,因为在iPhone上播放音乐和音效确 ...
- springboot 配置文件说明
你可以在自己创建的组件上使用@ConfigurationProperties注解,而Spring Boot自动配置的很多组件也添加了@ConfigurationProperties注解,可以通过Spr ...
- 修改eclipse的背景色(转载)
eclipse操作界面默认颜色为白色.对于我们长期使用电脑编程的人来说,白色很刺激我们的眼睛,所以我经常会改变workspace的背景色,使眼睛舒服一些. 设置方法如下: 1.打开window-> ...
- Luncene介绍
1.Luncene介绍 案例:实现一个文件的搜索功能,通过关键字搜索文件,凡是文件名或文件内容包括关键字文件都需要找出来.还可以根据中文词语进行查询,并且需要支持多个条件查询.Lucene可以解决 数 ...
- C#使用Aforge调用摄像头拍照
一. 新建一个Winform项目 二.使用Nuget添加引用 安装下图中红色框住的两个程序包 安装完后发现安装了如下图的程序包,这是因为上述两个程序包存在对其它程序包的依赖. 三.编写程序 1. 窗体 ...
- 设计模式--Proxy
转自:http://blog.csdn.net/dan_xp/article/details/1820852 最近一直在看java的设计模式 ,感觉印象最深刻的就是"面向接口编程" ...
- SQLite基本操作-----IOS(如有雷同,纯属巧合)
一.常用方法 sqlite3 *db, 数据库句柄,跟文件句柄FILE很类似 sqlite3_stmt *stmt, 这个相当于ODBC的Command对象,用于保存编译好 ...
- 计算机基础-BIOS
BIOS--硬件和软件的纽带(Basic Input Output System) 1.含义:基本的输入输出系统,它是一组固化到计算机内主板上的一个ROM存储芯片上的程序 2.性质:它保存着计算机最重 ...
- RBAC权限管理系统数据模型
懒得多写了,懂的看建表脚本就懂了... -- ---------------------------- -- Table structure for ucb_user -- ------------- ...