leetcode oj java Implement Stack using Queues
来源:互联网 发布:mac这么下载阿里旺旺 编辑:程序博客网 时间:2024/05/19 19:16
问题描述:
Implement Stack using Queues
Implement the following operations of a stack using queues.
- push(x) -- Push element x onto stack.
- pop() -- Removes the element on top of the stack.
- top() -- Get the top element.
- empty() -- Return whether the stack is empty.
- You must use only standard operations of a queue -- which means only
push to back
,peek/pop from front
,size
, andis empty
operations are valid. - Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.
- You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).
Update (2015-06-11):
The class name of the Java function had been updated to MyStack instead of Stack.
思路: 用两个队列实现一个栈 q1 作为专用的队列,q2作为中转站
出栈:将q1的前q1.size()-1个依次放入q2中,poll()出q1中剩下的一个,然后再将q2的依次放入q1中。
top : 如果队列中只有一个元素,取出即可; 如果队列中有多个元素,先将q1.size()-1个依次放入q2中,取出q1中剩下的,作为top值返回,再依次还原。
empty: 队列的isEmpty() 方法,
package leetcode;
import java.util.LinkedList;
import java.util.Queue;
public class MyStack {
Queue<Integer> q1 = new LinkedList<Integer>();
Queue<Integer> q2 = new LinkedList<Integer>();
// Push element x onto stack.
public void push(int x) {
q1.offer(x);
}
// Removes the element on top of the stack.
public void pop() {
int a = q1.size();
if(a==1){ q1.poll();}
else{
for(int i = 0; i<a-1; i++){
q2.add(q1.peek());
q1.poll();
}
q1.poll();
int b = a;
for(int i = 0; i<b-1; i++){
q1.add(q2.peek());
q2.poll();
}}
}
// Get the top element.
public int top() {
int a = 0;
if(q1.size()==1){a = q1.peek();}
else{
int m = q1.size();
for(int i = 0; i<m-1; i++){
q2.add(q1.peek());
q1.poll();
}
a = q1.peek();
q2.add(q1.peek());
q1.poll();
int n = q2.size();
for(int i = 0; i<n; i++){
q1.add(q2.peek());
q2.poll();
}}
// int a = q1.peek();
return a;
}
// Return whether the stack is empty.
public boolean empty() {
return q1.isEmpty();
}
}
提交的状态:
Accepted
/----------------------------我是华丽丽的分割线--------------------------/
自己编写了主函数测试:
package leetcode;
public class Stack {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
MyStack a = new MyStack();
a.push(1);
a.push(2);
a.push(3);
System.out.println(a.top());
a.pop();
System.out.println(a.top());
}
}
- leetcode oj java Implement Stack using Queues
- leetcode oj java Implement Stack using Queues
- <LeetCode OJ> 225. Implement Stack using Queues
- 【LeetCode OJ 225】Implement Stack using Queues
- 【leetcode】Implement Stack using Queues 【java】
- leetcode-java-225. Implement Stack using Queues
- [LeetCode]Implement Stack using Queues
- [LeetCode] Implement Stack using Queues
- [leetcode] Implement Stack using Queues
- LeetCode Implement Stack using Queues
- #leetcode#Implement Stack using Queues
- Leetcode: Implement Stack using Queues
- [LeetCode] Implement Stack using Queues
- [Leetcode]Implement Stack using Queues
- leetcode--Implement Stack using Queues
- LeetCode Implement Stack using Queues
- LeetCode || Implement Stack using Queues
- Leetcode Implement Stack using Queues
- LoadRunner主要功能
- 嵌入式底层软件开发学习系列之五调试方法
- ubuntu官方简体中文wiki,有你想要的ubuntu的一切
- <蓝牙BLE>cc2540主机读RSSI值
- 数据库事务的四大特性:ACID
- leetcode oj java Implement Stack using Queues
- kafka详解一、Kafka简介
- 二维码
- 机器学习笔记_数学基础_1-微积分
- 工业 4.0 时代,你准备好了吗?
- Qt从零开始制作串口调试助手-(第九章、串口读数据实现)-Creator_Ly
- 将《Android应用开发揭秘》例子源码添加进Eclipse
- 多标识AR程序分析(一)
- 《leetCode》:Jump Game II