deno.land / x / masx200_leetcode_test@10.6.5 / implement-queue-using-stacks-lcci / index.ts
12345678910111213141516171819202122232425262728293031323334353637383940414243// deno-lint-ignore no-explicit-anyclass MyQueue<T = any> { #inStack: Array<T> = []; #outStack: Array<T> = []; push(value: T): void { this.#inStack.push(value); }
pop(): number | T { if (!this.#outStack.length) { if (!this.#inStack.length) { return -1; } this.in2out(); } return this.#outStack.pop() as T; } in2out() { while (this.#inStack.length) { this.#outStack.push(this.#inStack.pop() as T); } } peek(): T | number { if (!this.#outStack.length) { if (!this.#inStack.length) { return -1; } this.in2out(); } return this.#outStack[this.#outStack.length - 1] as T; }
empty(): boolean { if (!this.#outStack.length) { if (!this.#inStack.length) { return true; } } return false; }}export default MyQueue;
Version Info