-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEasy_232_ImpQueueUseStack.kt
More file actions
44 lines (36 loc) · 973 Bytes
/
Easy_232_ImpQueueUseStack.kt
File metadata and controls
44 lines (36 loc) · 973 Bytes
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
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.boycoder.problems.stack
/**
* @Author: zhutao
* @datetime: 2021/6/22
* @desc:
*/
object Easy_232_ImpQueueUseStack {
/** Initialize your data structure here. */
val outStk = ArrayDeque<Int>()
val inStk = ArrayDeque<Int>()
/** Push element x to the back of queue. */
fun push(x: Int) {
inStk.addLast(x)
}
/** Removes the element from in front of queue and returns that element. */
fun pop(): Int {
if (empty()) {
return 0 // throw exception
}
// make sure in stack empty
while (!inStk.isEmpty()) {
outStk.addLast(inStk.removeLast())
}
return outStk.removeLast()
}
/** Get the front element. */
fun peek(): Int {
var value = pop()
outStk.addLast(value)
return value
}
/** Returns whether the queue is empty. */
fun empty(): Boolean {
return inStk.isEmpty() && outStk.isEmpty()
}
}