Skip to content

Commit 90f849b

Browse files
committed
新增 LeetCode 225 用队列实现栈的 C++ 实现,包含两种方法及详细注释;新增 AcWing 15 二维数组中的查找的 C++ 实现及相关笔记
1 parent aba3cb4 commit 90f849b

6 files changed

Lines changed: 412 additions & 2 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* @acwing app=acwing.cn id=16 lang=C++
3+
*
4+
* 15. 二维数组中的查找
5+
*/
6+
7+
// @acwing code start
8+
9+
class Solution {
10+
public:
11+
bool searchArray(vector<vector<int>> array, int target) {
12+
if(array.empty() || array[0].empty())return false;
13+
int i = 0,j = array[0].size() - 1;
14+
while(i < array.size() && j >= 0){
15+
if(array[i][j] == target)return true;
16+
if(array[i][j] > target)j--;
17+
else i++;
18+
}
19+
return false;
20+
}
21+
};
22+
// @acwing code end
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<!--
2+
* @Author: tkzzzzzz6
3+
* @Date: 2026-04-15 22:25:30
4+
* @LastEditors: tkzzzzzz6
5+
* @LastEditTime: 2026-04-15 22:41:33
6+
-->
7+
8+
需要注意的是这道题前一行的最后一个元素不一定比下一行的第一个元素小,所以不能直接使用二分查找。正确的做法是从右上角开始查找,如果当前元素大于目标值,向左移动,排除当前列;如果当前元素小于目标值,向下移动,排除当前行。直到找到目标值或者越界。
9+
10+
思路: 从右上角开始查找,如果当前元素大于目标值,向左移动,排除当前列;如果当前元素小于目标值,向下移动,排除当前行。直到找到目标值或者越界。
11+
12+
时间复杂度 O(m+n),其中 m 和 n 分别是二维数组的行数和列数。
13+
14+
```cpp
15+
/*
16+
* @acwing app=acwing.cn id=16 lang=C++
17+
*
18+
* 15. 二维数组中的查找
19+
*/
20+
21+
class Solution {
22+
public:
23+
bool searchArray(vector<vector<int>> array, int target) {
24+
if(array.empty() || array[0].empty())return false;
25+
int i = 0,j = array[0].size() - 1;
26+
while(i < array.size() && j >= 0){
27+
if(array[i][j] == target)return true;
28+
if(array[i][j] > target)j--;
29+
else i++;
30+
}
31+
return false;
32+
}
33+
};
34+
35+
```
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<!--
2+
* @Author: tkzzzzzz6
3+
* @Date: 2026-04-14 21:36:45
4+
* @LastEditors: tkzzzzzz6
5+
* @LastEditTime: 2026-04-14 22:15:55
6+
-->
7+
8+
使用队列,实现栈的功能
9+
10+
方法 1:两个队列,一个队列用来操作,另一个队列用来备份
11+
```cpp
12+
/*
13+
* @Author: tkzzzzzz6
14+
* @Date: 2026-04-14 15:13:11
15+
* @LastEditors: tkzzzzzz6
16+
* @LastEditTime: 2026-04-14 21:14:52
17+
*/
18+
class MyStack {
19+
public:
20+
queue<int> q1,q2; //q2用于备份
21+
MyStack() {
22+
23+
}
24+
25+
void push(int x) {
26+
q1.push(x);
27+
}
28+
29+
int pop() {
30+
int n = q1.size();
31+
if(n < 1)return -1;
32+
--n;
33+
while(n--){
34+
q2.push(q1.front());
35+
q1.pop();
36+
}
37+
38+
int res = q1.front();
39+
q1.pop();
40+
41+
q1 = q2;
42+
while(!q2.empty())q2.pop();
43+
return res;
44+
}
45+
46+
int top() {
47+
int n = q1.size();
48+
if(n < 1)return -1;
49+
--n;
50+
while(n--){
51+
q2.push(q1.front());
52+
q1.pop();
53+
}
54+
55+
int res = q1.front();
56+
q2.push(q1.front());
57+
58+
q1 = q2;
59+
while(!q2.empty())q2.pop();
60+
return res;
61+
}
62+
63+
bool empty() {
64+
return q1.empty();
65+
}
66+
};
67+
```
68+
方法 2:一个队列,每次入栈时,元素出队重新排队
69+
```cpp
70+
class MyStack {
71+
public:
72+
queue<int> q1;
73+
MyStack() {
74+
75+
}
76+
77+
void push(int x) {
78+
q1.push(x);
79+
}
80+
81+
int pop() {
82+
int n = q1.size();
83+
if(n < 1)return -1;
84+
--n;
85+
while(n--){
86+
q1.push(q1.front());
87+
q1.pop();
88+
}
89+
90+
int res = q1.front();
91+
q1.pop();
92+
return res;
93+
}
94+
95+
int top() {
96+
int n = q1.size();
97+
if(n < 1)return -1;
98+
--n;
99+
while(n--){
100+
q1.push(q1.front());
101+
q1.pop();
102+
}
103+
104+
int res = q1.front();
105+
q1.push(q1.front());
106+
q1.pop();
107+
return res;
108+
}
109+
ƒ
110+
bool empty() {
111+
return q1.empty();
112+
}
113+
};
114+
```
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
---
2+
title: 2026-04-14-LeetCode刷题笔记-225-用队列实现栈
3+
categories:
4+
- 算法蒟蒻的成长记录
5+
- LeetCode
6+
date: 2026-04-14
7+
tags:
8+
-
9+
- 队列
10+
- LeetCode-225
11+
- 数据结构模拟
12+
---
13+
14+
# 2026-04-14-LeetCode刷题笔记-225-用队列实现栈
15+
16+
这道题的目标是:只使用队列操作,实现栈的后进先出行为。
17+
18+
---
19+
20+
## 方法一:双队列
21+
22+
核心思路:
23+
24+
1. `q1` 作为主队列,`q2` 作为备份队列。
25+
2. `pop/top` 时将 `q1``n-1` 个元素移动到 `q2`
26+
3. 此时 `q1` 队头就是栈顶元素。
27+
4. `pop` 直接删除该元素。
28+
5. `top` 读取该元素后再放回 `q2`,保证结构不变。
29+
30+
```cpp
31+
class MyStack {
32+
public:
33+
queue<int> q1, q2;
34+
35+
void push(int x) {
36+
q1.push(x);
37+
}
38+
39+
int pop() {
40+
if (q1.empty()) return -1;
41+
int n = q1.size();
42+
while (--n) {
43+
q2.push(q1.front());
44+
q1.pop();
45+
}
46+
47+
int res = q1.front();
48+
q1.pop();
49+
50+
swap(q1, q2);
51+
while (!q2.empty()) q2.pop();
52+
return res;
53+
}
54+
55+
int top() {
56+
if (q1.empty()) return -1;
57+
int n = q1.size();
58+
while (--n) {
59+
q2.push(q1.front());
60+
q1.pop();
61+
}
62+
63+
int res = q1.front();
64+
q2.push(q1.front());
65+
q1.pop();
66+
67+
swap(q1, q2);
68+
while (!q2.empty()) q2.pop();
69+
return res;
70+
}
71+
72+
bool empty() {
73+
return q1.empty();
74+
}
75+
};
76+
```
77+
78+
---
79+
80+
## 方法二:单队列
81+
82+
核心思路:
83+
84+
1. 所有元素都在 `q1`
85+
2. `pop/top` 时把前 `n-1` 个元素循环到队尾。
86+
3. 剩下的队头就是栈顶。
87+
4. `top` 取值后要放回队尾,保证不删除。
88+
89+
```cpp
90+
class MyStack {
91+
public:
92+
queue<int> q1;
93+
94+
void push(int x) {
95+
q1.push(x);
96+
}
97+
98+
int pop() {
99+
if (q1.empty()) return -1;
100+
int n = q1.size();
101+
while (--n) {
102+
q1.push(q1.front());
103+
q1.pop();
104+
}
105+
106+
int res = q1.front();
107+
q1.pop();
108+
return res;
109+
}
110+
111+
int top() {
112+
if (q1.empty()) return -1;
113+
int n = q1.size();
114+
while (--n) {
115+
q1.push(q1.front());
116+
q1.pop();
117+
}
118+
119+
int res = q1.front();
120+
q1.push(q1.front());
121+
q1.pop();
122+
return res;
123+
}
124+
125+
bool empty() {
126+
return q1.empty();
127+
}
128+
};
129+
```
130+
131+
---
132+
133+
## 复杂度
134+
135+
1. `push`:$O(1)$
136+
2. `pop`:$O(n)$
137+
3. `top`:$O(n)$
138+
4. `empty`:$O(1)$
139+
140+
---
141+
142+
## 总结
143+
144+
这题的关键不是 API 写法,而是明确语义:`top` 只能读不能删,`pop` 才删除。把这个区别处理对,队列模拟栈就很稳了。

leetcode/Study Plan/225. 用队列实现栈/2.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @Author: tkzzzzzz6
33
* @Date: 2026-04-14 15:13:11
44
* @LastEditors: tkzzzzzz6
5-
* @LastEditTime: 2026-04-14 21:35:34
5+
* @LastEditTime: 2026-04-15 22:13:33
66
*/
77
/*
88
* @lc app=leetcode.cn id=225 lang=cpp
@@ -19,7 +19,7 @@ using namespace std;
1919
#include <array>
2020
#include <bitset>
2121
#include <climits>
22-
#include <deque>
22+
#finclude <deque>
2323
#include <functional>
2424
#include <iostream>
2525
#include <list>

0 commit comments

Comments
 (0)