deno.land / x / masx200_leetcode_test@10.6.5 / implement-queue-using-stacks-lcci / index.ts

نووسراو ببینە
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
// 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;
masx200_leetcode_test

Version Info

Tagged at
a year ago