본문 바로가기

Algorithm/Implementation

(C++) - LeetCode (easy) 225. Implement Stack using Queues

반응형

https://leetcode.com/problems/implement-stack-using-queues/description/

 

Implement Stack using Queues - LeetCode

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 el

leetcode.com

class를 활용한 자료구조 문제였습니다.

📕 풀이방법

📔 풀이과정

1. class의 생성자에 매개변수가 없으므로 MyStack 생성자는 필요가 없습니다. MyStack이 실체화되면 stack st를 생성해줍니다.

2. 동작 방식에 따라 member 함수를 구현해줍니다.


📕 Code

📔 C++

class MyStack {
public:
    stack <int> st;
    
    void push(int x) {
        st.push(x);
    }
    
    int pop() {
        int top = st.top();
        st.pop();
        return top;
    }
    
    int top() {
        return st.top();
    }
    
    bool empty() {
        return st.empty();
    }
};

/**
 * 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();
 */

*더 나은 내용을 위한 지적, 조언은 언제나 환영합니다.