N

(Leet Code c++)Implement Stack using Queues 본문

Leet Code 알고리즘

(Leet Code c++)Implement Stack using Queues

naeunchan 2021. 7. 26. 10:18
728x90
반응형

225. Implement Stack using Queues

 

Implement a last-in-first-out (LIFO) stack using only two queues. The implemented stack should support all the functions of a normal stack (push, top, pop, and empty).

Implement the MyStack class:

  • void push(int x) Pushes element x to the top of the stack.
  • int pop() Removes the element on the top of the stack and returns it.
  • int top() Returns the element on the top of the stack.
  • boolean empty() Returns true if the stack is empty, false otherwise.

Notes:

  • You must use only standard operations of a queue, which means that only push to back, peek/pop from front, size and is empty operations are valid.
  • Depending on your language, the queue may not be supported natively. You may simulate a queue using a list or deque (double-ended queue) as long as you use only a queue's standard operations.

 

Example 1:

Input ["MyStack", "push", "push", "top", "pop", "empty"] [[], [1], [2], [], [], []] Output [null, null, null, 2, 2, false] Explanation MyStack myStack = new MyStack(); myStack.push(1); myStack.push(2); myStack.top(); // return 2 myStack.pop(); // return 2 myStack.empty(); // return False

 

Constraints:

  • 1 <= x <= 9
  • At most 100 calls will be made to push, pop, top, and empty.
  • All the calls to pop and top are valid.

큐를 이용해 스택 자료구조를 구현.

private으로 int형 큐를 하나 선언하여 이를 활용한다.

 

-push

큐의 맨 뒤에 x를 push

 

-pop

임시 큐를 하나를 선언.(tmpQ)

q의 마지막 원소를 빼고 모두 tmpQ에 push.

q에 하나 남아 있는 원소는 res 변수에 저장한 후, q = tmpQ로 하여 pop한 결과를 다시 저장한다.

그리고 res를 리턴.

 

-top

queue의 back() 함수를 이용.

 

-empty

queue의 empty() 함수를 이용.

class MyStack {
private:
    queue<int> q;
    
public:
    /** Initialize your data structure here. */
    MyStack() {
        while(!q.empty()){
            q.pop();
        }
    }
    
    /** Push element x onto stack. */
    void push(int x) {
        q.push(x);
    }
    
    /** Removes the element on top of the stack and returns that element. */
    int pop() {
        queue<int> tmpQ;
        int res = 0;
        
        while(q.size() != 1){
            tmpQ.push(q.front());
            q.pop();
        }
        
        res = q.front();
        q = tmpQ;
        
        return res;
    }
    
    /** Get the top element. */
    int top() {
        return q.back();
    }
    
    /** Returns whether the stack is empty. */
    bool empty() {
        if(q.empty()){
            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();
 * bool param_4 = obj->empty();
 */

 

728x90
반응형

'Leet Code 알고리즘' 카테고리의 다른 글

(Leet Code c++)Power of Two  (0) 2021.07.27
(Leet Code c++)Invert Binary Tree  (0) 2021.07.26
(Leet Code c++)Contains Duplicate(2)  (0) 2021.07.23
(Leet Code c++)Contains Duplicate  (0) 2021.07.23
(Leet Code c++)Reverse Linked List  (0) 2021.07.23