Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2e49b5b

Browse files
feat: update 4 leetcode about queue and stack
1 parent 66932d6 commit 2e49b5b

File tree

2 files changed

+202
-0
lines changed

2 files changed

+202
-0
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/*
2+
* @lc app=leetcode id=225 lang=typescript
3+
*
4+
* [225] Implement Stack using Queues
5+
*/
6+
7+
// @lc code=start
8+
// class ArrayQueue<number> {
9+
// private length: number; // 队列长度
10+
// private queue: number[]; // 队列
11+
// private head: number; // 队首指针
12+
// private tail: number; // 队尾指针
13+
14+
// constructor(n: number) {
15+
// this.length = n;
16+
// this.queue = [];
17+
// this.head = 0;
18+
// this.tail = 0;
19+
// }
20+
21+
// /**
22+
// * 入队
23+
// * 返回是否入队成功
24+
// * @param item
25+
// * @returns
26+
// */
27+
// enqueue(item: number): boolean {
28+
// if (this.tail === this.length) {
29+
// return false;
30+
// }
31+
// this.queue[this.tail] = item;
32+
// this.tail++;
33+
// return true;
34+
// }
35+
36+
// /**
37+
// * 出队操作
38+
// * 返回队首值的同时,将指针往后移
39+
// * @returns
40+
// */
41+
// dequeue(): number | undefined | null {
42+
// if (this.head === this.tail) {
43+
// return null;
44+
// }
45+
// const item = this.queue[this.head];
46+
// this.head++;
47+
// return item;
48+
// }
49+
// }
50+
51+
/**
52+
* @description: 两个队列实现栈
53+
* @param {number}
54+
* @return {MyStack}
55+
*/
56+
class MyStack {
57+
private outQueue: number[]; // 用于保存栈的弹出顺序
58+
private inQueue: number[];
59+
60+
constructor() {
61+
this.outQueue = [];
62+
this.inQueue = [];
63+
}
64+
65+
/**
66+
* @description: 通过两次转换使得队列顺序变成栈的顺序
67+
* [3, 1], [] => [3, 1], [5] => [], [5, 3, 1] => [5, 3, 1], []
68+
* @return {void}
69+
*/
70+
in2out(): void {
71+
while (this.outQueue.length) {
72+
this.inQueue.push(this.outQueue.shift());
73+
}
74+
while (this.inQueue.length) {
75+
this.outQueue.push(this.inQueue.shift());
76+
}
77+
}
78+
79+
/**
80+
* @description: 往栈中推入数据
81+
* @param {number} x
82+
* @return {void}
83+
*/
84+
push(x: number): void {
85+
this.inQueue.push(x);
86+
this.in2out();
87+
}
88+
89+
/**
90+
* @description: 栈中弹出数据
91+
* @return {number}
92+
*/
93+
pop(): number {
94+
return this.outQueue.shift();
95+
}
96+
97+
/**
98+
* @description: 取栈尾的数据
99+
* @return {number}
100+
*/
101+
top(): number {
102+
return this.outQueue[0];
103+
}
104+
105+
/**
106+
* @description: 栈是否为空
107+
* @return {boolean}
108+
*/
109+
empty(): boolean {
110+
return !this.outQueue.length;
111+
}
112+
}
113+
114+
/**
115+
* Your MyStack object will be instantiated and called as such:
116+
* var obj = new MyStack()
117+
* obj.push(x)
118+
* var param_2 = obj.pop()
119+
* var param_3 = obj.top()
120+
* var param_4 = obj.empty()
121+
*/
122+
// @lc code=end
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* @lc app=leetcode id=232 lang=typescript
3+
*
4+
* [232] Implement Queue using Stacks
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* @description: 双栈思想
10+
*/
11+
class MyQueue {
12+
private inStack: number[];
13+
private outStack: number[];
14+
15+
constructor() {
16+
this.inStack = [];
17+
this.outStack = [];
18+
}
19+
20+
/**
21+
* @description: 从第一个栈里弹出数据到第二个栈,此时第二个栈再弹出就是队列的顺序
22+
* @return {void}
23+
*/
24+
private in2out(): void {
25+
while (this.inStack.length) {
26+
this.outStack.push(this.inStack.pop());
27+
}
28+
}
29+
30+
/**
31+
* @description: 往队末推入元素
32+
* @param {number} x
33+
* @return {void}
34+
*/
35+
public push(x: number): void {
36+
this.inStack.push(x);
37+
}
38+
39+
/**
40+
* @description: 弹出队首元素
41+
* @return {number}
42+
*/
43+
public pop(): number {
44+
if (!this.outStack.length) {
45+
this.in2out();
46+
}
47+
48+
return this.outStack.pop();
49+
}
50+
51+
/**
52+
* @description: 取队首元素
53+
* @return {number}
54+
*/
55+
public peek(): number {
56+
if (!this.outStack.length) {
57+
this.in2out();
58+
}
59+
60+
return this.outStack[this.outStack.length - 1];
61+
}
62+
63+
/**
64+
* @description: 队列是否为空
65+
* @return {boolean}
66+
*/
67+
public empty(): boolean {
68+
return this.inStack.length === 0 && this.outStack.length === 0;
69+
}
70+
}
71+
72+
/**
73+
* Your MyQueue object will be instantiated and called as such:
74+
* var obj = new MyQueue()
75+
* obj.push(x)
76+
* var param_2 = obj.pop()
77+
* var param_3 = obj.peek()
78+
* var param_4 = obj.empty()
79+
*/
80+
// @lc code=end

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /