码迷,mamicode.com
首页 > 其他好文 > 详细

用栈实现队列

时间:2019-05-04 00:09:20      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:leetcode   front   new   from   http   ret   .com   return   public   

 

用栈实现队列 - 力扣(LeetCode)
https://leetcode-cn.com/problems/implement-queue-using-stacks/

 

https://leetcode.com/problems/implement-queue-using-stacks/solution/

 

class MyQueue {
Stack<Integer> queue = new Stack<Integer>();
// Push element x to the back of queue.
public void push(int x) {
Stack<Integer> temp = new Stack<Integer>();
while(!queue.empty()){
temp.push(queue.pop());
}
queue.push(x);
while(!temp.empty()){
queue.push(temp.pop());
}
}

// Removes the element from in front of queue.
public int pop() {
return queue.pop();
}

// Get the front element.
public int peek() {
return queue.peek();
}

// Return whether the queue is empty.
public boolean empty() {
return queue.empty();
}
}

 

用栈实现队列

标签:leetcode   front   new   from   http   ret   .com   return   public   

原文地址:https://www.cnblogs.com/yuanjiangw/p/10807069.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!