LeetCode--255--用队列实现栈(java版)

时间:2021-10-17 12:20:02

使用队列实现栈的下列操作:

  • push(x) -- 元素 x 入栈
  • pop() -- 移除栈顶元素
  • top() -- 获取栈顶元素
  • empty() -- 返回栈是否为空

注意:

  • 你只能使用队列的基本操作-- 也就是 push to backpeek/pop from frontsize, 和 is empty 这些操作是合法的。
  • 你所使用的语言也许不支持队列。 你可以使用 list 或者 deque(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。
  • 你可以假设所有操作都是有效的(例如, 对一个空的栈不会调用 pop 或者 top 操作)。

超级慢的。

 class MyStack {
private LinkedList<Integer> list1 ;
private LinkedList<Integer> list2 ;
/** Initialize your data structure here. */
public MyStack() {
this.list1 = new LinkedList<Integer>();
this.list2 = new LinkedList<Integer>();
} /** Push element x onto stack. */
public void push(int x) {
list1.add(x);
} /** Removes the element on top of the stack and returns that element. */
public int pop() {
int res = 0;
if(list1.size()==1){
return list1.poll();
}else{
while(list1.size()!=1){
list2.add(list1.poll());
}
res = list1.poll();
while(!list2.isEmpty()){
list1.add(list2.poll());
}
}
return res;
} /** Get the top element. */
public int top() {
int res = 0;
if(list1.size() == 1){
res = list1.peek(); }else{
while(list1.size()!=1){
list2.add(list1.poll());
}
res = list1.peek();
list2.add(list1.poll());
while(!list2.isEmpty()){
list1.add(list2.poll());
}
}
return res;
} /** Returns whether the stack is empty. */
public boolean empty() {
if(list1.isEmpty() && list2.isEmpty()){
return true;
}else{
return false;
}
}
} /**
* 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();
*/

这个比较快:

 class MyStack {

     List<Integer> queue1=new LinkedList();
List<Integer> queue2=new LinkedList();
private int num=1;
/** Initialize your data structure here. */
public MyStack() { } /** Push element x onto stack. */
public void push(int x) {
if(num==1) {
queue1.add(x);
}else {
queue2.add(x);
}
} /** Removes the element on top of the stack and returns that element. */
public int pop() {
if(num==1) {
while(queue1.size()!=1) {
queue2.add(queue1.remove(0));
}
num=2;
return queue1.remove(0);
}else {
while(queue2.size()!=1) {
queue1.add(queue2.remove(0));
}
num=1;
return queue2.remove(0);
}
} /** Get the top element. */
public int top() {
if(num==1) {
return queue1.get(queue1.size()-1);
}else {
return queue2.get(queue2.size()-1);
}
} /** Returns whether the stack is empty. */
public boolean empty() {
if(num==1) {
return queue1.isEmpty();
}else {
return queue2.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();
*/

最简单:

 class MyStack {
private Queue<Integer> data; public MyStack() {
data = new LinkedList<>();
} public void push(int x) {
data.offer(x);
//将队列中前面已经逆序的元素放在x元素后面,使得整体逆序
for (int i = 0; i < data.size() - 1; i++) {
data.offer(data.poll());
}
} public int pop() {
return data.poll();
} public int top() {
return data.peek();
} public boolean empty() {
return data.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();
*/

2019-03-04 23:00:41