首页 > 编程语言 > 详细

LeetCode-225 Implement Stack using Queues Solution (with Java)

时间:2020-03-02 14:42:04      阅读:61      评论:0      收藏:0      [点我收藏+]

1. Description:

技术分享图片

Notes:

技术分享图片

2. Examples:

技术分享图片

3.Solutions:

 1 /**
 2  * Created by sheepcore on 2019-05-07
 3  * Your MyStack object will be instantiated and called as such:
 4  * MyStack obj = new MyStack();
 5  * obj.push(x);
 6  * int param_2 = obj.pop();
 7  * int param_3 = obj.top();
 8  * boolean param_4 = obj.empty();
 9  */
10 class MyStack {
11     /** Initialize your data structure here. */
12    private Queue<Integer> queue = new LinkedList<Integer>();
13 
14 
15     /** Push element x onto stack. */
16     public void push(int x) {
17         queue.add(x);
18         for(int i = 1; i < queue.size(); i++)
19             queue.add(queue.remove());
20     }
21 
22     /** Removes the element on top of the stack and returns that element. */
23     public int pop() {
24         return queue.remove();
25     }
26 
27     /** Get the top element. */
28     public int top() {
29         return queue.peek();
30     }
31 
32     /** Returns whether the stack is empty. */
33     public boolean empty() {
34         return queue.isEmpty();
35     }
36 }

 

LeetCode-225 Implement Stack using Queues Solution (with Java)

原文:https://www.cnblogs.com/sheepcore/p/12395227.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!