Skip to content

Latest commit

 

History

History
38 lines (30 loc) · 1 KB

leetcode_232.md

File metadata and controls

38 lines (30 loc) · 1 KB

LeetCode Problems

232. Implement Queue using Stacks

class MyQueue() {
    // implements queue using 2 stacks
    private val front = Stack<Int>()
    private val back = Stack<Int>()
    // front-forward, move all elements in back to front. and newly push.
    fun push(x: Int) {
        while (back.isNotEmpty()) front.push(back.pop())
        front.push(x)
    }
    // back-forward, move all elements in front to back. return pop() of back.
    fun pop(): Int {
        while (front.isNotEmpty()) back.push(front.pop())
        return back.pop()
    }
    // back-forward, move all elements in front to back. return peek() of back.
    fun peek(): Int {
        while (front.isNotEmpty()) back.push(front.pop())
        return back.peek()
    }
    // check are front and back stack all empty.
    fun empty(): Boolean = front.isEmpty() && back.isEmpty()
}