[LeetCode] 225. Implement Stack using Queues

[LeetCode] 225. Implement Stack using Queues

Kotlin

Queue 1개 사용

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class MyStack() {

/** Initialize your data structure here. */
val q1: Queue<Int> = LinkedList<Int>()

/** Push element x onto stack. */
fun push(x: Int) {
val size = q1.size
q1.offer(x)
for (i in 0 until size) {
q1.offer(q1.poll())
}
}

/** Removes the element on top of the stack and returns that element. */
fun pop(): Int {
return q1.poll()
}

/** Get the top element. */
fun top(): Int {
return q1.peek()
}

/** Returns whether the stack is empty. */
fun empty(): Boolean {
return q1.isEmpty()
}

}

Queue 2개 사용

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class MyStack() {

/** Initialize your data structure here. */
val q1: Queue<Int> = LinkedList<Int>()
val q2: Queue<Int> = LinkedList<Int>()

/** Push element x onto stack. */
fun push(x: Int) {
q1.add(x);
while (!q2.isEmpty())
q1.add(q2.poll());
while (!q1.isEmpty())
q2.add(q1.poll());
}

/** Removes the element on top of the stack and returns that element. */
fun pop(): Int {
return q2.poll()
}

/** Get the top element. */
fun top(): Int {
return q2.peek()
}

/** Returns whether the stack is empty. */
fun empty(): Boolean {
return q1.isEmpty() && q2.isEmpty()
}

}

댓글