Skip to content

Commit f1ca5e1

Browse files
committed
Merge branch 'main' of github.com:tkzzzzzz6/Algorithm_beginner_learning_notes
2 parents f74220d + adc99db commit f1ca5e1

23 files changed

Lines changed: 964 additions & 12 deletions

acwing/problems/.cpp

Lines changed: 0 additions & 10 deletions
This file was deleted.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* @acwing app=acwing.cn id=45 lang=C++
3+
*
4+
* 47. 二叉树中和为某一值的路径
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>> res;
21+
vector<int> tracker;
22+
vector<vector<int>> findPath(TreeNode *root, int sum) {
23+
dfs(root, sum);
24+
return res;
25+
}
26+
27+
void dfs(TreeNode *root, int sum) {
28+
if (!root)
29+
return;
30+
sum -= root->val;
31+
tracker.push_back(root->val);
32+
if (!root->right && !root->left && !sum) {
33+
res.push_back(tracker);
34+
}
35+
dfs(root->left, sum);
36+
dfs(root->right, sum);
37+
tracker.pop_back();
38+
}
39+
};
40+
// @acwing code end
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* @acwing app=acwing.cn id=89 lang=C++
3+
*
4+
* 48. 复杂链表的复刻
5+
*/
6+
7+
// @acwing code start
8+
9+
/**
10+
* Definition for singly-linked list with a random pointer.
11+
* struct ListNode {
12+
* int val;
13+
* ListNode *next, *random;
14+
* ListNode(int x) : val(x), next(NULL), random(NULL) {}
15+
* };
16+
*/
17+
class Solution {
18+
public:
19+
ListNode *copyRandomList(ListNode *head) {
20+
if (!head)
21+
return head;
22+
23+
// 1.在每个原节点后面复制新节点
24+
auto p = head;
25+
while (p) {
26+
auto np = new ListNode(p->val);
27+
np->next = p->next;
28+
p->next = np;
29+
p = np->next;
30+
}
31+
32+
// 2.设置新节点的 random 指针
33+
p = head;
34+
while (p) {
35+
if (p->random) {
36+
p->next->random = p->random->next;
37+
}
38+
p = p->next->next;
39+
}
40+
41+
// 3.拆分链表
42+
auto dummy = new ListNode(-1);
43+
auto cur = dummy;
44+
p = head;
45+
while (p) {
46+
cur->next = p->next;
47+
cur = cur->next;
48+
p->next = p->next->next;
49+
p = p->next;
50+
}
51+
52+
return dummy->next;
53+
}
54+
};
55+
56+
// @acwing code end
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* @Author: tkzzzzzz6
3+
* @Date: 2026-04-25 11:33:20
4+
* @LastEditors: tkzzzzzz6
5+
* @LastEditTime: 2026-04-25 11:36:00
6+
*/
7+
/*
8+
* @acwing app=acwing.cn id=87 lang=C++
9+
*
10+
* 49. 二叉搜索树与双向链表
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+
#include <utility>
25+
using namespace std;
26+
class Solution {
27+
public:
28+
TreeNode *convert(TreeNode *root) {
29+
if (!root)
30+
return nullptr;
31+
auto res = dfs(root);
32+
return res.first;
33+
}
34+
35+
pair<TreeNode *, TreeNode *> dfs(TreeNode *root) {
36+
if (!root->left && !root->right)
37+
return {root, root};
38+
if (root->left && root->right) {
39+
auto lslide = dfs(root->left), rslide = dfs(root->right);
40+
lslide.second->right = root, root->left = lslide.second;
41+
rslide.first->left = root, root->right = rslide.first;
42+
return {lslide.first, rslide.second};
43+
}
44+
if (root->left) {
45+
auto lslide = dfs(root->left);
46+
lslide.second->right = root, root->left = lslide.second;
47+
return {lslide.first, root};
48+
}
49+
if (root->right) {
50+
auto rslide = dfs(root->right);
51+
rslide.first->left = root, root->right = rslide.first;
52+
return {root, rslide.second};
53+
}
54+
55+
return {nullptr, nullptr};
56+
}
57+
};
58+
// @acwing code end
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* @acwing app=acwing.cn id=46 lang=C++
3+
*
4+
* 50. 序列化二叉树
5+
*/
6+
7+
// @acwing code start
8+
9+
#include <sstream>
10+
#include <string>
11+
12+
using namespace std;
13+
14+
/**
15+
* Definition for a binary tree node.
16+
* struct TreeNode {
17+
* int val;
18+
* TreeNode *left;
19+
* TreeNode *right;
20+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
21+
* };
22+
*/
23+
class Solution {
24+
public:
25+
// Encodes a tree to a single string.
26+
string serialize(TreeNode *root) {
27+
string s;
28+
dfs_s(root, s);
29+
return s;
30+
}
31+
32+
void dfs_s(TreeNode *root, string &s) {
33+
if (!root) {
34+
s += "n ";
35+
return;
36+
} else {
37+
s += to_string(root->val) + ' ';
38+
}
39+
dfs_s(root->left, s);
40+
dfs_s(root->right, s);
41+
}
42+
43+
// Decodes your encoded data to tree.
44+
TreeNode *deserialize(string data) {
45+
stringstream ss(data);
46+
return dfs_d(ss);
47+
}
48+
49+
TreeNode *dfs_d(stringstream &ss) {
50+
string s;
51+
ss >> s;
52+
if (s == "n")
53+
return nullptr;
54+
55+
auto p = new TreeNode(stoi(s));
56+
p->left = dfs_d(ss);
57+
p->right = dfs_d(ss);
58+
59+
return p;
60+
}
61+
};
62+
// @acwing code end
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* @acwing app=acwing.cn id=47 lang=C++
3+
*
4+
* 51. 数字排列
5+
*/
6+
7+
// @acwing code start
8+
9+
#include <vector>
10+
class Solution {
11+
public:
12+
vector<vector<int>> res;
13+
vector<int> path;
14+
vector<vector<int>> permutation(vector<int> &nums) {
15+
path.resize(nums.size());
16+
sort(nums.begin(), nums.end());
17+
dfs(nums, 0, 0, 0);
18+
return res;
19+
}
20+
void dfs(vector<int> &nums, int u, int start, int state) {
21+
if (u == nums.size()) {
22+
res.push_back(path);
23+
return;
24+
}
25+
if (!u || nums[u] != nums[u - 1])
26+
start = 0;
27+
for (int i = start; i < nums.size(); ++i) {
28+
if (!(state >> i & 1)) {
29+
path[i] = nums[u];
30+
dfs(nums, u + 1, i + 1, state + (1 << i));
31+
}
32+
}
33+
}
34+
};
35+
// @acwing code end
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* @acwing app=acwing.cn id=48 lang=C++
3+
*
4+
* 52. 数组中出现次数超过一半的数字
5+
*/
6+
7+
// @acwing code start
8+
9+
class Solution {
10+
public:
11+
int moreThanHalfNum_Solution(vector<int> &nums) {
12+
int cnt = 0, val = -1;
13+
for (int i = 0; i < nums.size(); ++i) {
14+
if (cnt <= 0)
15+
val = nums[i];
16+
if (nums[i] == val)
17+
cnt++;
18+
else
19+
cnt--;
20+
}
21+
return val;
22+
}
23+
};
24+
// @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=49 lang=C++
3+
*
4+
* 53. 最小的k个数
5+
*/
6+
7+
// @acwing code start
8+
9+
#include <algorithm>
10+
#include <queue>
11+
#include <vector>
12+
class Solution {
13+
public:
14+
vector<int> getLeastNumbers_Solution(vector<int> input, int k) {
15+
std::priority_queue<int> heap;
16+
std::vector<int> res;
17+
18+
for (auto x : input) {
19+
heap.push(x);
20+
if (heap.size() > k)
21+
heap.pop();
22+
}
23+
24+
while (!heap.empty()) {
25+
res.push_back(heap.top());
26+
heap.pop();
27+
}
28+
std::reverse(res.begin(), res.end());
29+
return res;
30+
}
31+
};
32+
// @acwing code endnt
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* @acwing app=acwing.cn id=88 lang=C++
3+
*
4+
* 54. 数据流中的中位数
5+
*/
6+
7+
// @acwing code start
8+
9+
#include <functional>
10+
#include <vector>
11+
class Solution {
12+
public:
13+
priority_queue<int> max_heap; // 大顶堆,存储较小的一半
14+
priority_queue<int, std::vector<int>, std::greater<int>> min_heap; // 小顶堆,存储较大的一半
15+
void insert(int num) {
16+
max_heap.push(num);
17+
if (min_heap.size() && max_heap.top() > min_heap.top()) {
18+
auto maxv = max_heap.top(), minv = min_heap.top();
19+
max_heap.pop(), min_heap.pop();
20+
max_heap.push(minv), min_heap.push(maxv);
21+
}
22+
if (max_heap.size() > min_heap.size() + 1) {
23+
min_heap.push(max_heap.top());
24+
max_heap.pop();
25+
}
26+
}
27+
28+
double getMedian() {
29+
if (max_heap.size() + min_heap.size() & 1) {
30+
return max_heap.top();
31+
} else {
32+
return (max_heap.top() + min_heap.top()) / 2.0;
33+
}
34+
}
35+
};
36+
// @acwing code end
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* @acwing app=acwing.cn id=50 lang=C++
3+
*
4+
* 55. 连续子数组的最大和
5+
*/
6+
7+
// @acwing code start
8+
9+
#include <climits>
10+
class Solution {
11+
public:
12+
int maxSubArray(vector<int> &nums) {
13+
int res = INT_MIN, s = 0;
14+
for (auto x : nums) {
15+
if (s < 0)
16+
s = 0;
17+
s += x;
18+
res = max(res, s);
19+
}
20+
return res;
21+
}
22+
};
23+
// @acwing code end

0 commit comments

Comments
 (0)