Skip to content

Commit f48c9d5

Browse files
committed
feat: leetcode 225
1 parent 9893495 commit f48c9d5

File tree

7 files changed

+156
-0
lines changed

7 files changed

+156
-0
lines changed

assets/data/category.json

+6
Original file line numberDiff line numberDiff line change
@@ -1647,6 +1647,12 @@
16471647
"path": "../../problemset/basic-calculator/README.md",
16481648
"difficulty": "困难"
16491649
},
1650+
{
1651+
"id": 225,
1652+
"title": "用队列实现栈",
1653+
"path": "../../problemset/implement-stack-using-queues/README.md",
1654+
"difficulty": "简单"
1655+
},
16501656
{
16511657
"id": 2104,
16521658
"title": "子数组范围和",

assets/data/problems.json

+10
Original file line numberDiff line numberDiff line change
@@ -1999,6 +1999,16 @@
19991999
"url": "https://leetcode-cn.com/problems/basic-calculator/",
20002000
"path": "../../problemset/basic-calculator/README.md"
20012001
},
2002+
{
2003+
"id": 225,
2004+
"title": {
2005+
"cn": "用队列实现栈",
2006+
"en": "implement-stack-using-queues"
2007+
},
2008+
"difficulty": "简单",
2009+
"url": "https://leetcode-cn.com/problems/implement-stack-using-queues/",
2010+
"path": "../../problemset/implement-stack-using-queues/README.md"
2011+
},
20022012
{
20032013
"id": 258,
20042014
"title": {

assets/docs/CATEGORY.md

+1
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@
324324
| 150. [逆波兰表达式求值](../../problemset/valuate-reverse-polish-notation/README.md) | 中等 |
325325
| 155. [最小栈](../../problemset/min-stack/README.md) | 简单 |
326326
| 224. [基本计算器](../../problemset/basic-calculator/README.md) | 困难 |
327+
| 225. [用队列实现栈](../../problemset/implement-stack-using-queues/README.md) | 简单 |
327328
| 2104. [子数组范围和](../../problemset/sum-of-subarray-ranges/README.md) | 中等 |
328329

329330
## 递归 & 迭代

assets/docs/PROBLEMS.md

+2
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,8 @@
400400

401401
[224. 基本计算器](../../problemset/basic-calculator/README.md)
402402

403+
[225. 用队列实现栈](../../problemset/implement-stack-using-queues/README.md)
404+
403405
[258. 各位相加](../../problemset/add-digits/README.md)
404406

405407
[300. 最长递增子序列](../../problemset/longest-increasing-subsequence/README.md)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# 用队列实现栈
2+
3+
> 难度:简单
4+
>
5+
> https://leetcode-cn.com/problems/implement-stack-using-queues/
6+
7+
## 题目
8+
9+
请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通栈的全部四种操作(`push``top``pop``empty`)。
10+
11+
实现 `MyStack` 类:
12+
13+
- `void push(int x)` 将元素 `x` 压入栈顶。
14+
- `int pop()` 移除并返回栈顶元素。
15+
- `int top()` 返回栈顶元素。
16+
- `boolean empty()` 如果栈是空的,返回 `true` ;否则,返回 `false`
17+
 
18+
19+
注意:
20+
21+
- 你只能使用队列的基本操作 —— 也就是 `push to back``peek/pop from front``size` 和 `is empty` 这些操作。
22+
- 你所使用的语言也许不支持队列。 你可以使用 `list` (列表)或者 `deque`(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。
23+
 
24+
25+
### 示例:
26+
27+
```
28+
输入:
29+
["MyStack", "push", "push", "top", "pop", "empty"]
30+
[[], [1], [2], [], [], []]
31+
输出:
32+
[null, null, null, 2, 2, false]
33+
34+
解释:
35+
MyStack myStack = new MyStack();
36+
myStack.push(1);
37+
myStack.push(2);
38+
myStack.top(); // 返回 2
39+
myStack.pop(); // 返回 2
40+
myStack.empty(); // 返回 False
41+
```
42+
43+
## 解题
44+
45+
```ts
46+
export class MyStack<T = number> {
47+
queue = new Array<T>()
48+
49+
/**
50+
* 入栈
51+
* @param x
52+
*/
53+
push(x: T): void {
54+
let len = this.queue.length
55+
this.queue.unshift(x)
56+
while (len) {
57+
this.queue.unshift(this.queue.pop()!)
58+
len--
59+
}
60+
}
61+
62+
/**
63+
* 出栈
64+
* @returns
65+
*/
66+
pop(): T {
67+
return this.queue.pop()!
68+
}
69+
70+
/**
71+
* 获取栈顶元素
72+
* @returns
73+
*/
74+
top(): T {
75+
return this.queue[this.queue.length - 1]
76+
}
77+
78+
/**
79+
* 判断栈是否为空
80+
* @returns
81+
*/
82+
empty(): boolean {
83+
return this.queue.length === 0
84+
}
85+
}
86+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { expect, it } from 'vitest'
2+
import { MyStack } from '.'
3+
4+
it('用队列实现栈', () => {
5+
const stack = new MyStack<Number>()
6+
stack.push(1)
7+
stack.push(2)
8+
expect(stack.top()).toBe(2)
9+
expect(stack.pop()).toBe(2)
10+
expect(stack.empty()).toBe(false)
11+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
export class MyStack<T = number> {
2+
queue = new Array<T>()
3+
4+
/**
5+
* 入栈
6+
* @param x
7+
*/
8+
push(x: T): void {
9+
let len = this.queue.length
10+
this.queue.unshift(x)
11+
while (len) {
12+
this.queue.unshift(this.queue.pop()!)
13+
len--
14+
}
15+
}
16+
17+
/**
18+
* 出栈
19+
* @returns
20+
*/
21+
pop(): T {
22+
return this.queue.pop()!
23+
}
24+
25+
/**
26+
* 获取栈顶元素
27+
* @returns
28+
*/
29+
top(): T {
30+
return this.queue[this.queue.length - 1]
31+
}
32+
33+
/**
34+
* 判断栈是否为空
35+
* @returns
36+
*/
37+
empty(): boolean {
38+
return this.queue.length === 0
39+
}
40+
}

0 commit comments

Comments
 (0)