Skip to content

Commit 3efd178

Browse files
committed
feat: 添加多个算法题的解决方案及相关代码实现
1 parent 675b440 commit 3efd178

16 files changed

Lines changed: 553 additions & 0 deletions

acwing/problems/.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/*
2+
* @acwing app=acwing.cn id=38 lang=C++
3+
*
4+
*
5+
*/
6+
7+
// @acwing code start
8+
9+
10+
// @acwing code end
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* @Author: tkzzzzzz6
3+
* @Date: 2026-04-22 08:01:54
4+
* @LastEditors: tkzzzzzz6
5+
* @LastEditTime: 2026-04-22 08:09:06
6+
*/
7+
/*
8+
* @acwing app=acwing.cn id=34 lang=C++
9+
*
10+
* 36. 合并两个排序的链表
11+
*/
12+
13+
// @acwing code start
14+
15+
/**
16+
* Definition for singly-linked list.
17+
* struct ListNode {
18+
* int val;
19+
* ListNode *next;
20+
* ListNode(int x) : val(x), next(NULL) {}
21+
* };
22+
*/
23+
class Solution {
24+
public:
25+
ListNode* merge(ListNode* l1, ListNode* l2) {
26+
auto dummy = new ListNode(0);
27+
auto cur = dummy;
28+
29+
while(l1 && l2){
30+
if(l1->val > l2->val){
31+
cur->next = l2;
32+
l2 = l2->next;
33+
}else{
34+
cur->next = l1;
35+
l1 = l1->next;
36+
}
37+
cur = cur->next;
38+
}
39+
40+
if(l1){
41+
cur->next = l1;
42+
}else{
43+
cur->next = l2;
44+
}
45+
46+
return dummy->next;
47+
}
48+
};
49+
// @acwing code end
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* @acwing app=acwing.cn id=35 lang=C++
3+
*
4+
* 37. 树的子结构
5+
*/
6+
7+
// @acwing code start
8+
9+
/**
10+
* Definition for a binary tree node.
11+
* struct TreeNode {
12+
* int val;
13+
* TreeNode *left;
14+
* TreeNode *right;
15+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
16+
* };
17+
*/
18+
class Solution {
19+
public:
20+
bool hasSubtree(TreeNode* pRoot1, TreeNode* pRoot2) {
21+
if(!pRoot1 || !pRoot2)return false;
22+
if(isSame(pRoot1,pRoot2))return true;
23+
return hasSubtree(pRoot1->left,pRoot2) || hasSubtree(pRoot1->right,pRoot2);
24+
}
25+
26+
bool isSame(TreeNode* root1,TreeNode* root2){
27+
if(!root2)return true;
28+
if(!root1 || root1->val != root2->val)return false;
29+
return isSame(root1->left,root2->left) && isSame(root1->right,root2->right);
30+
}
31+
};
32+
// @acwing code end
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* @acwing app=acwing.cn id=37 lang=C++
3+
*
4+
* 38. 二叉树的镜像
5+
*/
6+
7+
// @acwing code start
8+
9+
/**
10+
* Definition for a binary tree node.
11+
* struct TreeNode {
12+
* int val;
13+
* TreeNode *left;
14+
* TreeNode *right;
15+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
16+
* };
17+
*/
18+
class Solution {
19+
public:
20+
void mirror(TreeNode* root) {
21+
if(!root)return;
22+
swap(root->left,root->right);
23+
mirror(root->left);
24+
mirror(root->right);
25+
}
26+
};
27+
// @acwing code end
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* @Author: tkzzzzzz6
3+
* @Date: 2026-04-22 10:58:49
4+
* @LastEditors: tkzzzzzz6
5+
* @LastEditTime: 2026-04-22 11:01:05
6+
*/
7+
/*
8+
* @acwing app=acwing.cn id=38 lang=C++
9+
*
10+
* 39. 对称的二叉树
11+
*/
12+
13+
// @acwing code start
14+
15+
/**
16+
* Definition for a binary tree node.
17+
* struct TreeNode {
18+
* int val;
19+
* TreeNode *left;
20+
* TreeNode *right;
21+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
22+
* };
23+
*/
24+
class Solution {
25+
public:
26+
bool isSymmetric(TreeNode* root) {
27+
return !root || dfs(root->left,root->right);
28+
}
29+
30+
bool dfs(TreeNode* p,TreeNode* q){
31+
if(!p || !q)return !p && !q;
32+
33+
return (p->val == q->val) && dfs(p->left,q->right) && dfs(p->right,q->left);
34+
}
35+
};
36+
// @acwing code end
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* @acwing app=acwing.cn id=39 lang=C++
3+
*
4+
* 40. 顺时针打印矩阵
5+
*/
6+
7+
// @acwing code start
8+
9+
class Solution {
10+
public:
11+
vector<int> printMatrix(vector<vector<int> > matrix) {
12+
if(matrix.empty() || matrix[0].empty())return vector<int>();
13+
14+
vector<int> res;
15+
int n = matrix.size(),m = matrix[0].size();
16+
int dx[4] = {0,1,0,-1},dy[4] = {1,0,-1,0};
17+
18+
int d = 0,x = 0,y = 0;
19+
vector<vector<bool>> vis(n,vector<bool>(m));
20+
21+
for(int k = 0;k<n*m;++k){
22+
res.push_back(matrix[x][y]);
23+
vis[x][y] = true;
24+
25+
int a = x + dx[d];
26+
int b = y + dy[d];
27+
28+
if(a<0 || a >= n || b < 0 || b >= m || vis[a][b]){
29+
d = (d+1) %4;
30+
a = x+dx[d],b = y + dy[d];
31+
}
32+
x = a,y = b;
33+
}
34+
return res;
35+
}
36+
};
37+
// @acwing code end
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* @acwing app=acwing.cn id=90 lang=C++
3+
*
4+
* 41. 包含min函数的栈
5+
*/
6+
7+
// @acwing code start
8+
9+
class MinStack {
10+
public:
11+
/** initialize your data structure here. */
12+
stack<int> st,stmin;
13+
MinStack() {
14+
15+
}
16+
17+
void push(int x) {
18+
st.push(x);
19+
if(stmin.empty()){
20+
stmin.push(x);
21+
}else{
22+
stmin.push(min(x,stmin.top()));
23+
}
24+
}
25+
26+
void pop() {
27+
if(st.empty())return;
28+
st.pop();
29+
stmin.pop();
30+
}
31+
32+
int top() {
33+
return st.empty() ? -1 : st.top();
34+
}
35+
36+
int getMin() {
37+
return stmin.empty() ? -1 : stmin.top();
38+
}
39+
};
40+
41+
/**
42+
* Your MinStack object will be instantiated and called as such:
43+
* MinStack obj = new MinStack();
44+
* obj.push(x);
45+
* obj.pop();
46+
* int param_3 = obj.top();
47+
* int param_4 = obj.getMin();
48+
*/
49+
// @acwing code end
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* @acwing app=acwing.cn id=40 lang=C++
3+
*
4+
* 42. 栈的压入、弹出序列
5+
*/
6+
7+
// @acwing code start
8+
9+
class Solution {
10+
public:
11+
stack<int> st;
12+
bool isPopOrder(vector<int> pushV,vector<int> popV) {
13+
if(pushV.size() != popV.size())return false;
14+
15+
int j = 0;
16+
for(int x:pushV){
17+
st.push(x);
18+
while(!st.empty() && j < popV.size() && st.top() == popV[j]){
19+
st.pop();
20+
++j;
21+
}
22+
}
23+
24+
return j == popV.size();
25+
}
26+
};
27+
// @acwing code end
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* @acwing app=acwing.cn id=41 lang=C++
3+
*
4+
* 43. 不分行从上往下打印二叉树
5+
*/
6+
7+
// @acwing code start
8+
9+
/**
10+
* Definition for a binary tree node.
11+
* struct TreeNode {
12+
* int val;
13+
* TreeNode *left;
14+
* TreeNode *right;
15+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
16+
* };
17+
*/
18+
class Solution {
19+
public:
20+
vector<int> printFromTopToBottom(TreeNode* root) {
21+
vector<int> res;
22+
if(!root)return res;
23+
24+
queue<TreeNode*> q;
25+
q.push(root);
26+
27+
while(q.size()){
28+
auto t = q.front();
29+
q.pop();
30+
res.push_back(t->val);
31+
if(t->left)q.push(t->left);
32+
if(t->right)q.push(t->right);
33+
}
34+
35+
return res;
36+
}
37+
};
38+
// @acwing code end
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* @acwing app=acwing.cn id=42 lang=C++
3+
*
4+
* 44. 分行从上往下打印二叉树
5+
*/
6+
7+
// @acwing code start
8+
9+
/**
10+
* Definition for a binary tree node.
11+
* struct TreeNode {
12+
* int val;
13+
* TreeNode *left;
14+
* TreeNode *right;
15+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
16+
* };
17+
*/
18+
class Solution {
19+
public:
20+
vector<vector<int>> printFromTopToBottom(TreeNode* root) {
21+
vector<vector<int>> res;
22+
if(!root)return res;
23+
24+
queue<TreeNode*> q;
25+
q.push(root);
26+
q.push(nullptr);
27+
28+
vector<int> level;
29+
while(q.size()){
30+
auto t = q.front();
31+
q.pop();
32+
33+
if(t == nullptr){
34+
res.push_back(level);
35+
level.clear();
36+
if(!q.empty()){
37+
q.push(nullptr);
38+
}
39+
}else{
40+
level.push_back(t->val);
41+
if(t->left)q.push(t->left);
42+
if(t->right)q.push(t->right);
43+
}
44+
}
45+
46+
return res;
47+
}
48+
};
49+
// @acwing code end

0 commit comments

Comments
 (0)