반응형
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();
*/
*더 나은 내용을 위한 지적, 조언은 언제나 환영합니다.
'Algorithm > Implementation' 카테고리의 다른 글
(C++) - LeetCode (easy) 242. Valid Anagram (0) | 2023.01.20 |
---|---|
(C++) - LeetCode (easy) 228. Summary Ranges (0) | 2023.01.12 |
(C++) - LeetCode (easy) 219. Contains Duplicate II (0) | 2023.01.09 |
(C++) - LeetCode (easy) 217. Contains Duplicate (0) | 2023.01.08 |
(C++) - LeetCode (easy) 171. Excel Sheet Column Number (0) | 2022.12.11 |