码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2020-03-02 14:58:48      阅读:75      评论:0      收藏:0      [点我收藏+]

标签:move   obj   solution   alt   strong   not   bool   whether   leetcode   

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)

标签:move   obj   solution   alt   strong   not   bool   whether   leetcode   

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

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