Advertisement
1988coder

225. Implement Stack using Queues

Jan 5th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.89 KB | None | 0 0
  1.  
  2. // LeetCode URL: https://leetcode.com/problems/implement-stack-using-queues/
  3. import java.util.LinkedList;
  4. import java.util.Queue;
  5.  
  6. /**
  7.  * One Queue. Push is O(N), Pop is O(1).
  8.  *
  9.  * Time Complexity: Push = O(N). Pop = O(1). Top = O(1). Empty = O(1)
  10.  *
  11.  * Space Complexity: O(N)
  12.  *
  13.  * N = Number of elements in the stack.
  14.  */
  15. class MyStack1 {
  16.  
  17.     Queue<Integer> queue;
  18.  
  19.     /** Initialize your data structure here. */
  20.     public MyStack1() {
  21.         queue = new LinkedList<>();
  22.     }
  23.  
  24.     /** Push element x onto stack. */
  25.     public void push(int x) {
  26.         queue.offer(x);
  27.         for (int i = 0; i < queue.size() - 1; i++) {
  28.             queue.offer(queue.poll());
  29.         }
  30.     }
  31.  
  32.     /** Removes the element on top of the stack and returns that element. */
  33.     public int pop() throws RuntimeException {
  34.         if (queue.isEmpty()) {
  35.             throw new RuntimeException("Stack is empty");
  36.         }
  37.         return queue.poll();
  38.     }
  39.  
  40.     /** Get the top element. */
  41.     public int top() throws RuntimeException {
  42.         if (queue.isEmpty()) {
  43.             throw new RuntimeException("Stack is empty");
  44.         }
  45.         return queue.peek();
  46.     }
  47.  
  48.     /** Returns whether the stack is empty. */
  49.     public boolean empty() {
  50.         return queue.isEmpty();
  51.     }
  52. }
  53.  
  54. /**
  55.  * Two Queue. Push is O(1), Pop is O(N).
  56.  *
  57.  * Time Complexity: Push = O(1). Pop = O(N). Top = O(1). Empty = O(1)
  58.  *
  59.  * Space Complexity: O(N)
  60.  *
  61.  * N = Number of elements in the stack.
  62.  */
  63. class MyStack2 {
  64.  
  65.     Queue<Integer> queue1;
  66.     Queue<Integer> queue2;
  67.     int top;
  68.  
  69.     /** Initialize your data structure here. */
  70.     public MyStack2() {
  71.         queue1 = new LinkedList<>();
  72.         queue2 = new LinkedList<>();
  73.     }
  74.  
  75.     /** Push element x onto stack. */
  76.     public void push(int x) {
  77.         queue1.offer(x);
  78.         top = x;
  79.     }
  80.  
  81.     /** Removes the element on top of the stack and returns that element. */
  82.     public int pop() throws RuntimeException {
  83.         if (queue1.isEmpty()) {
  84.             throw new RuntimeException("Stack is empty");
  85.         }
  86.         while (queue1.size() > 1) {
  87.             top = queue1.poll();
  88.             queue2.offer(top);
  89.         }
  90.         int poppedVal = queue1.poll();
  91.         Queue<Integer> temp = queue1;
  92.         queue1 = queue2;
  93.         queue2 = temp;
  94.         return poppedVal;
  95.     }
  96.  
  97.     /** Get the top element. */
  98.     public int top() throws RuntimeException {
  99.         if (queue1.isEmpty()) {
  100.             throw new RuntimeException("Stack is empty");
  101.         }
  102.         return top;
  103.     }
  104.  
  105.     /** Returns whether the stack is empty. */
  106.     public boolean empty() {
  107.         return queue1.isEmpty();
  108.     }
  109. }
  110.  
  111. // Your MyStack object will be instantiated and called as such:
  112. // MyStack obj = new MyStack();
  113. // obj.push(x);
  114. // int param_2 = obj.pop();
  115. // int param_3 = obj.top();
  116. // boolean param_4 = obj.empty();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement