Notice
Recent Posts
Recent Comments
Link
.Zzumbong
[leetCode/JS] 232. Implement Queue using Stacks ๋ณธ๋ฌธ
๋์ด๋ [ ๐ ] Easy
๋ฌธ์ ์ค๋ช
Implement a first in first out (FIFO) queue using only two stacks. The implemented queue should support all the functions of a normal queue (push
, peek
, pop
, and empty
).
Implement the MyQueue
class:
void push(int x)
Pushes element x to the back of the queue.int pop()
Removes the element from the front of the queue and returns it.int peek()
Returns the element at the front of the queue.boolean empty()
Returnstrue
if the queue is empty,false
otherwise.
Notes:
- You must use only standard operations of a stack, which means only
push to top
,peek/pop from top
,size
, andis empty
operations are valid. - Depending on your language, the stack may not be supported natively. You may simulate a stack using a list or deque (double-ended queue) as long as you use only a stack's standard operations.
์คํ 2๊ฐ๋ฅผ ์ด์ฉํด์ ์ ์ ์ ์ถ์ ํ๋ queue๋ฅผ ๋ง๋ค๊ธฐ์ด๋ค.
์ธ์ด์ ๋ฐ๋ผ์ ์คํ์ด๋ผ๋ ํ์ค ์คํผ๋ ์ด์ ์ด ์์ง๋ง.. js์ ์๋ค.
์ ์ถ๋ ฅ ์
Example 1:
Input
["MyQueue", "push", "push", "peek", "pop", "empty"]
[[], [1], [2], [], [], []]
Output
[null, null, null, 1, 1, false]
Explanation
MyQueue myQueue = new MyQueue();
myQueue.push(1); // queue is: [1]
myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue)
myQueue.peek(); // return 1
myQueue.pop(); // return 1, queue is [2]
myQueue.empty(); // return false
Constraints
1 <= x <= 9
- At most
100
calls will be made topush
,pop
,peek
, andempty
. - All the calls to
pop
andpeek
are valid.
๋ด ์๋ฃจ์
- ๋ฌธ์ ๋ฅผ ๋ฌด์ํ๊ณ js์์ ๊ทธ๋ฅ queue๋ฅผ ๋ง๋ค๋ฉด ์ด๋ ๊ฒ ๋๋ค.
- ์ด๋ฐ๊ฑธ ์ํ๊ฑด ์๋๊ฒ ์ง..
class MyQueue {
constructor() {
this.queue = [];
}
push(n) {
if(!n) return;
this.queue.push(n);
}
pop() {
return this.queue.shift();
}
peek() {
return this.queue[0];
}
empty() {
return this.queue.length === 0;
}
}
- ์ง์ง๋ก stack 2๊ฐ๋ฅผ ์ด์ฉํ ์ ์ ์ ์ถ ๋ฐฉ์.
- push๋ก ์์๋ฅผ ์ ๋ง๋ค๋ฉด ๋๋จธ์ง ์ฝ๋ค.
class MyQueue {
constructor() {
this.stack = [];
this.temp = [];
}
push(n) {
while (this.stack.length > 0) { // ๋ฉ์ธ ์คํ ๋ฐ์ดํฐ๋ฅผ temp ์คํ์ผ๋ก ์ฎ๊น.
this.temp.push(this.stack.pop());
}
this.temp.push(n); // temp์ ๋ง์ง๋ง์ n์ ๋ฃ์.
while (this.temp.length > 0) { // temp๋ฅผ ์ญ์์ผ๋ก stack์ ๋ฃ์.
this.stack.push(this.temp.pop());
}
}
pop() {
return this.stack.pop();
}
peek() {
return this.stack[this.stack.length-1];
}
empty() {
return this.stack.length === 0;
}
}
const st = new MyQueue();
st.push(1);
st.push(2);
st.push(3);
// stack : [3, 2, 1], temp: []
๊ฐ์ํ
- ๋ค๋ฅธ ์ธ์ด์์ stack ์ด๋ผ๋ ๋ฐ์ดํฐํ์ด ์กด์ฌํ๋๋ณด๋ค.
- ๊ทผ๋ฐ JS์์ ์ด๋ด ์ด์ ๊ฐ 1๋ ์์ด์ ์ ~~๋ง ํ๊ณ ์ถ์ง ์์๋ ๋ฌธ์ .
'coding test > leetCode' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[leetCode/JS] 739. Daily Temperatures (0) | 2022.12.18 |
---|---|
[leetCode/JS] 150. Evaluate Reverse Polish Notation (0) | 2022.12.17 |
[leetCode/JS] 1143. Longest Common Subsequence (0) | 2022.12.15 |
[leetCode/JS] 152. Maximum Product Subarray (0) | 2022.12.14 |
[leetCode/JS] 198. House Robber (0) | 2022.12.14 |
0 Comments