Skip to content

Commit 2a545b5

Browse files
committed
一刷225
1 parent ad0ca9f commit 2a545b5

File tree

5 files changed

+159
-28
lines changed

5 files changed

+159
-28
lines changed

README.adoc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1601,12 +1601,12 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
16011601
|Hard
16021602
|
16031603

1604-
//|{counter:codes}
1605-
//|{leetcode_base_url}/implement-stack-using-queues/[225. Implement Stack using Queues^]
1606-
//|{source_base_url}/_0225_ImplementStackUsingQueues.java[Java]
1607-
//|{doc_base_url}/0225-implement-stack-using-queues.adoc[题解]
1608-
//|Easy
1609-
//|
1604+
|{counter:codes}
1605+
|{leetcode_base_url}/implement-stack-using-queues/[225. Implement Stack using Queues^]
1606+
|{source_base_url}/_0225_ImplementStackUsingQueues.java[Java]
1607+
|{doc_base_url}/0225-implement-stack-using-queues.adoc[题解]
1608+
|Easy
1609+
|
16101610

16111611
|{counter:codes}
16121612
|{leetcode_base_url}/invert-binary-tree/[226. Invert Binary Tree^]
Lines changed: 58 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,80 @@
11
[#0225-implement-stack-using-queues]
2-
= 225. Implement Stack using Queues
2+
= 225. 用队列实现栈
33

4-
{leetcode}/problems/implement-stack-using-queues/[LeetCode - Implement Stack using Queues^]
4+
https://leetcode.cn/problems/implement-stack-using-queues/[LeetCode - 225. 用队列实现栈 ^]
55

6-
Implement the following operations of a stack using queues.
6+
请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通栈的全部四种操作(`push``top``pop``empty`)。
77

8+
实现 `MyStack` 类:
89

9-
* push(x) -- Push element x onto stack.
10-
* pop() -- Removes the element on top of the stack.
11-
* top() -- Get the top element.
12-
* empty() -- Return whether the stack is empty.
10+
* `void push(int x)` 将元素 x 压入栈顶。
11+
* `int pop()` 移除并返回栈顶元素。
12+
* `int top()` 返回栈顶元素。
13+
* `boolean empty()` 如果栈是空的,返回 `true` ;否则,返回 `false`
1314
15+
*注意:*
1416

15-
*Example:*
17+
* 你只能使用队列的标准操作 —— 也就是 `push to back``peek/pop from front``size``is empty` 这些操作。
18+
* 你所使用的语言也许不支持队列。 你可以使用 list (列表)或者 deque(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。
1619
17-
[subs="verbatim,quotes,macros"]
18-
----
19-
MyStack stack = new MyStack();
20+
*示例:*
2021

21-
stack.push(1);
22-
stack.push(2);
23-
stack.top(); // returns 2
24-
stack.pop(); // returns 2
25-
stack.empty(); // returns false
26-
----
22+
....
23+
输入:
24+
["MyStack", "push", "push", "top", "pop", "empty"]
25+
[[], [1], [2], [], [], []]
26+
输出:
27+
[null, null, null, 2, 2, false]
28+
29+
解释:
30+
MyStack myStack = new MyStack();
31+
myStack.push(1);
32+
myStack.push(2);
33+
myStack.top(); // 返回 2
34+
myStack.pop(); // 返回 2
35+
myStack.empty(); // 返回 False
36+
....
37+
38+
39+
*提示:*
2740

28-
*Notes:*
41+
* `+1 <= x <= 9+`
42+
* 最多调用`100``push``pop``top``empty`
43+
* 每次调用 `pop``top` 都保证栈不为空
2944
3045
31-
* You must use _only_ standard operations of a queue -- which means only `push to back`, `peek/pop from front`, `size`, and `is empty` operations are valid.
32-
* Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.
33-
* You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).
46+
**进阶:**你能否仅用一个队列来实现栈。
3447

3548

49+
== 思路分析
3650

51+
就是用两个队列来回倒腾。添加元素的时候,哪个空就放那个队列中,把另外一个队列里的值都倒腾过来。
52+
53+
使用一个队列来求解,则在添加新记录之前,把队列长度记录下来,添加完新元素,再出队入队原来队列长度次数。
3754

3855
[[src-0225]]
56+
[tabs]
57+
====
58+
一刷::
59+
+
60+
--
3961
[{java_src_attr}]
4062
----
4163
include::{sourcedir}/_0225_ImplementStackUsingQueues.java[tag=answer]
4264
----
65+
--
66+
67+
// 二刷::
68+
// +
69+
// --
70+
// [{java_src_attr}]
71+
// ----
72+
// include::{sourcedir}/_0225_ImplementStackUsingQueues_2.java[tag=answer]
73+
// ----
74+
// --
75+
====
76+
77+
78+
== 参考资料
4379

80+
. https://leetcode.cn/problems/implement-stack-using-queues/solutions/432204/yong-dui-lie-shi-xian-zhan-by-leetcode-solution/[225. 用队列实现栈 - 官方题解^]

docs/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ include::0223-rectangle-area.adoc[leveloffset=+1]
535535

536536
include::0224-basic-calculator.adoc[leveloffset=+1]
537537

538-
// include::0225-implement-stack-using-queues.adoc[leveloffset=+1]
538+
include::0225-implement-stack-using-queues.adoc[leveloffset=+1]
539539

540540
include::0226-invert-binary-tree.adoc[leveloffset=+1]
541541

logbook/202503.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,6 +1113,11 @@ endif::[]
11131113
|{doc_base_url}/0223-rectangle-area.adoc[题解]
11141114
|⭕️ 原本还想按分离,包含和相交三种情况来处理,相交情况太多,放弃!容斥原理的处理手段和 {doc_base_url}/0000-04-merge-intervals.adoc[Merge Intervals 区间合并] 题目的方式类似!
11151115

1116+
|{counter:codes2503}
1117+
|{leetcode_base_url}/implement-stack-using-queues/[225. 用队列实现栈^]
1118+
|{doc_base_url}/0225-implement-stack-using-queues.adoc[题解]
1119+
|✅ 就是用两个队列来回倒腾。添加元素的时候,哪个空就放那个队列中,把另外一个队列里的值都倒腾过来。
1120+
11161121
|===
11171122

11181123
截止目前,本轮练习一共完成 {codes2503} 道题。
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import java.util.LinkedList;
4+
import java.util.Queue;
5+
6+
public class _0225_ImplementStackUsingQueues {
7+
// tag::answer[]
8+
/**
9+
* @author D瓜哥 · https://www.diguage.com
10+
* @since 2025-07-02 16:59:24
11+
*/
12+
class MyStack {
13+
14+
// 一个队列解法
15+
private Queue<Integer> queue;
16+
17+
public MyStack() {
18+
queue = new LinkedList<>();
19+
}
20+
21+
public void push(int x) {
22+
int size = queue.size();
23+
queue.offer(x);
24+
for (int i = 0; i < size; i++) {
25+
queue.offer(queue.poll());
26+
}
27+
}
28+
29+
public int pop() {
30+
return queue.poll();
31+
}
32+
33+
public int top() {
34+
return queue.peek();
35+
}
36+
37+
public boolean empty() {
38+
return queue.isEmpty();
39+
}
40+
41+
// // 两个队列的解法
42+
// private Queue<Integer> q1;
43+
// private Queue<Integer> q2;
44+
//
45+
// public MyStack() {
46+
// q1 = new LinkedList<>();
47+
// q2 = new LinkedList<>();
48+
// }
49+
//
50+
// public void push(int x) {
51+
// if (q1.isEmpty()) {
52+
// q1.offer(x);
53+
// while (!q2.isEmpty()) {
54+
// q1.offer(q2.poll());
55+
// }
56+
// } else {
57+
// q2.offer(x);
58+
// while (!q1.isEmpty()) {
59+
// q2.offer(q1.poll());
60+
// }
61+
// }
62+
// }
63+
//
64+
// public int pop() {
65+
// if (q1.isEmpty()) {
66+
// return q2.poll();
67+
// } else {
68+
// return q1.poll();
69+
// }
70+
// }
71+
//
72+
// public int top() {
73+
// if (q1.isEmpty()) {
74+
// return q2.peek();
75+
// } else {
76+
// return q1.peek();
77+
// }
78+
// }
79+
//
80+
// public boolean empty() {
81+
// if (q1.isEmpty()) {
82+
// return q2.isEmpty();
83+
// } else {
84+
// return q1.isEmpty();
85+
// }
86+
// }
87+
}
88+
// end::answer[]
89+
}

0 commit comments

Comments
 (0)