Skip to content

Commit 97ff05b

Browse files
authored
Merge pull request #73 from SilentSliver/master
Merge from master
2 parents d6738e5 + 404023d commit 97ff05b

File tree

231 files changed

+9403
-426
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

231 files changed

+9403
-426
lines changed

CHANGELOG.md

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,41 @@
11
# CHANGELOG
22

3+
## v10.1.0 (2025-06-12)
4+
5+
### Feature
6+
7+
* feat: cpp memory improve (#160)
8+
9+
* fix: c++ memory leaks
10+
11+
prevent memory leaks and improve memory management in ListNode and Node classes
12+
13+
* fix: c++ ListNode free
14+
15+
improve memory management in ListNode handling and prevent leaks
16+
17+
* fix: c++ tree memory
18+
19+
update daily problem ID and enhance memory management in solutions
20+
21+
* test: LCR_055 cpp memory
22+
23+
delete root
24+
25+
* test: add dev problem 2
26+
27+
dev problem 2
28+
29+
* feat: cpp node neightbor
30+
31+
implement graph cloning and memory management functions ([`340904a`](https://github.com/QuBenhao/LeetCode/commit/340904a506bc591acda97dd10bfc489b660cbd6e))
32+
33+
### Test
34+
35+
* test: 3574 solution
36+
37+
py, c++, go ([`b647870`](https://github.com/QuBenhao/LeetCode/commit/b64787041b5213966c3711e645a71c6ffd75b37c))
38+
339
## v10.0.2 (2025-06-12)
440

541
### Fix
@@ -100,22 +136,6 @@ folder after plan problem
100136

101137
Co-authored-by: GitHub Action <action@github.com> ([`98351ab`](https://github.com/QuBenhao/LeetCode/commit/98351abcc537eb38f90b25e798064df4b1ec416f))
102138

103-
### Test
104-
105-
* test: 3445 solution
106-
107-
c++, go, java ([`ad2c53d`](https://github.com/QuBenhao/LeetCode/commit/ad2c53d4c4f0b14c763f264a91611f4645c8b07d))
108-
109-
* test: 3445 solution
110-
111-
py ([`6deb184`](https://github.com/QuBenhao/LeetCode/commit/6deb184396515bb0d926b01f01af41cfec2c3c11))
112-
113-
* test: [20250611] Add (3445) ([`e76129d`](https://github.com/QuBenhao/LeetCode/commit/e76129d6ea562d47323f22ef276ed7ee82914025))
114-
115-
## v9.8.0 (2025-06-10)
116-
117-
### Breaking
118-
119139
* feat: leetcode script
120140

121141
add main script for problem management and submission
@@ -2784,6 +2804,16 @@ go.sum uploaded ([`064618d`](https://github.com/QuBenhao/LeetCode/commit/064618d
27842804

27852805
### Test
27862806

2807+
* test: 3445 solution
2808+
2809+
c++, go, java ([`ad2c53d`](https://github.com/QuBenhao/LeetCode/commit/ad2c53d4c4f0b14c763f264a91611f4645c8b07d))
2810+
2811+
* test: 3445 solution
2812+
2813+
py ([`6deb184`](https://github.com/QuBenhao/LeetCode/commit/6deb184396515bb0d926b01f01af41cfec2c3c11))
2814+
2815+
* test: [20250611] Add (3445) ([`e76129d`](https://github.com/QuBenhao/LeetCode/commit/e76129d6ea562d47323f22ef276ed7ee82914025))
2816+
27872817
* test: 3442 solution
27882818

27892819
py, c++, go, java ([`2e1fbe3`](https://github.com/QuBenhao/LeetCode/commit/2e1fbe3b583e9b187af03306055c01c03dc0cee6))

Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,10 @@ members = [
348348
"problems/problems_3442",
349349
"problems/problems_3445",
350350
"problems/problems_3423",
351+
"problems/problems_2616",
352+
"problems/problems_2566",
353+
"problems/problems_1432",
354+
"problems/problems_2016",
351355
]
352356

353357
[package]
@@ -718,3 +722,7 @@ solution_440 = { path = "problems/problems_440", features = ["solution_440"] }
718722
solution_3442 = { path = "problems/problems_3442", features = ["solution_3442"] }
719723
solution_3445 = { path = "problems/problems_3445", features = ["solution_3445"] }
720724
solution_3423 = { path = "problems/problems_3423", features = ["solution_3423"] }
725+
solution_2616 = { path = "problems/problems_2616", features = ["solution_2616"] }
726+
solution_2566 = { path = "problems/problems_2566", features = ["solution_2566"] }
727+
solution_1432 = { path = "problems/problems_1432", features = ["solution_1432"] }
728+
solution_2016 = { path = "problems/problems_2016", features = ["solution_2016"] }

cpp/models/ListNode.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,42 @@
44

55
#include "ListNode.h"
66

7-
ListNode *IntArrayToListNode(std::vector<int> &arr) {
8-
auto dummy = new ListNode(), p = dummy;
7+
ListNode *IntArrayToListNode(const std::vector<int> &arr) {
8+
ListNode dummy = ListNode();
9+
ListNode *p = &dummy;
910
for (auto val : arr) {
1011
p->next = new ListNode(val);
1112
p = p->next;
1213
}
13-
return dummy->next;
14+
p = dummy.next;
15+
dummy.next = nullptr; // Prevent memory leak
16+
return p;
1417
}
1518

16-
std::vector<int> &ListNodeToIntArray(ListNode *head) {
17-
auto *arr = new std::vector<int>();
19+
std::vector<int> ListNodeToIntArray(ListNode *head) {
20+
auto arr = std::vector<int>();
1821
while (head != nullptr) {
19-
arr->push_back(head->val);
22+
arr.push_back(head->val);
2023
head = head->next;
2124
}
22-
return *arr;
25+
return arr;
2326
}
2427

2528
ListNode *IntArrayToListNodeCycle(std::vector<int> &arr, int pos) {
26-
auto dummy = new ListNode(), p = dummy;
29+
ListNode dummy = ListNode();
30+
ListNode *p = &dummy;
2731
ListNode *cycle = nullptr;
28-
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
32+
for (size_t i = 0; i < arr.size(); ++i) {
2933
p->next = new ListNode(arr[i]);
3034
p = p->next;
3135
if (i == pos) {
3236
cycle = p;
3337
}
3438
}
3539
p->next = cycle;
36-
return dummy->next;
40+
p = dummy.next;
41+
dummy.next = nullptr; // Prevent memory leak
42+
return p;
3743
}
3844

3945
std::tuple<ListNode *, ListNode *>

cpp/models/ListNode.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ struct ListNode {
2323
}
2424
};
2525

26-
ListNode *IntArrayToListNode(std::vector<int> &arr);
26+
ListNode *IntArrayToListNode(const std::vector<int> &arr);
2727

28-
std::vector<int> &ListNodeToIntArray(ListNode *head);
28+
std::vector<int> ListNodeToIntArray(ListNode *head);
2929

3030
ListNode *IntArrayToListNodeCycle(std::vector<int> &arr, int pos);
3131

cpp/models/NodeNeighbors.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44

55
#include "NodeNeighbors.h"
66
#include <unordered_set>
7+
#include <queue>
78

8-
Node *JsonArrayToNodeNeighbors(vector<vector<int>> arr) {
9+
Node *JsonArrayToNodeNeighbors(const vector<vector<int>>& arr) {
910
if (arr.empty()) {
1011
return nullptr;
1112
}
12-
vector<Node *> nodes = vector<Node *>(arr.size() + 1);
13+
vector<Node *> nodes = vector<Node *>(arr.size() + 1, nullptr);
1314
for (size_t i = 1; i <= arr.size(); i++) {
1415
nodes[i] = new Node(static_cast<int>(i));
1516
}
@@ -52,4 +53,23 @@ vector<vector<int>> NodeNeighborsToJsonArray(Node *root) {
5253
visited.insert(root->val);
5354
dfs(root, ans, visited);
5455
return ans;
56+
}
57+
58+
void DeleteGraph(Node* root) {
59+
if (!root) return;
60+
std::unordered_set<Node*> visited;
61+
std::queue<Node*> q;
62+
q.push(root);
63+
visited.insert(root);
64+
while (!q.empty()) {
65+
Node* node = q.front();
66+
q.pop();
67+
for (Node* neighbor : node->neighbors) {
68+
if (neighbor && !visited.count(neighbor)) {
69+
visited.insert(neighbor);
70+
q.push(neighbor);
71+
}
72+
}
73+
delete node;
74+
}
5575
}

cpp/models/NodeNeighbors.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ class Node {
3434
}
3535
};
3636

37-
Node *JsonArrayToNodeNeighbors(vector<vector<int>> arr);
37+
Node *JsonArrayToNodeNeighbors(const vector<vector<int>>& arr);
3838
vector<vector<int>> NodeNeighborsToJsonArray(Node *root);
39+
void DeleteGraph(Node* root);
3940

4041
#endif //LEETCODE_NODENEIGHBORS_H

cpp/models/NodeRandom.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ class Node {
2323
next = nullptr;
2424
random = nullptr;
2525
}
26+
27+
~Node() {
28+
delete next; // Automatically delete the next node to avoid memory leaks
29+
}
2630
};
2731

2832
Node* JsonArrayToNodeRandom(json arr);

cpp/models/TreeNodeNext.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ class Node {
2323

2424
Node(int _val, Node* _left, Node* _right, Node* _next)
2525
: val(_val), left(_left), right(_right), next(_next) {}
26+
27+
~Node() {
28+
delete left;
29+
delete right;
30+
// next is not deleted here because it may point to another Node in the same tree
31+
}
2632
};
2733

2834
Node *JsonArrayToTreeNodeNext(json arr);

daily-problems.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
2-
"daily": "2767",
3-
"plans": ["1", "problems", "LCR_115", "problems"]
2+
"daily": "1206",
3+
"plans": ["3582", "problems", "3583", "problems", "3584", "problems", "3585", "problems"]
44
}

golang/problems_test.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
package golang
22

33
import (
4-
"leetCode/problems/problems_LCR_115"
4+
"leetCode/problems/problems_3582"
5+
"leetCode/problems/problems_3583"
6+
"leetCode/problems/problems_3584"
7+
"leetCode/problems/problems_3585"
58
"testing"
69
)
710

811
func TestSolutions(t *testing.T) {
9-
TestEach(t, "LCR_115", "problems", problemLCR_115.Solve)
12+
TestEach(t, "3582", "problems", problem3582.Solve)
13+
TestEach(t, "3583", "problems", problem3583.Solve)
14+
TestEach(t, "3584", "problems", problem3584.Solve)
15+
TestEach(t, "3585", "problems", problem3585.Solve)
1016
}

golang/solution_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package golang
22

33
import (
4-
problem "leetCode/problems/problems_2767"
4+
problem "leetCode/problems/problems_1206"
55
"testing"
66
)
77

88
func TestSolution(t *testing.T) {
9-
TestEach(t, "2767", "problems", problem.Solve)
9+
TestEach(t, "1206", "problems", problem.Solve)
1010
}

problems/problems_1041/problem_zh.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# 1041. 困于环中的机器人 [难度分: 1521.18]
2+
3+
<p>在无限的平面上,机器人最初位于&nbsp;<code>(0, 0)</code>&nbsp;处,面朝北方。注意:</p>
4+
5+
<ul>
6+
<li><strong>北方向</strong> 是y轴的正方向。</li>
7+
<li><strong>南方向</strong> 是y轴的负方向。</li>
8+
<li><strong>东方向</strong> 是x轴的正方向。</li>
9+
<li><strong>西方向</strong> 是x轴的负方向。</li>
10+
</ul>
11+
12+
<p>机器人可以接受下列三条指令之一:</p>
13+
14+
<ul>
15+
<li><code>"G"</code>:直走 1 个单位</li>
16+
<li><code>"L"</code>:左转 90 度</li>
17+
<li><code>"R"</code>:右转 90 度</li>
18+
</ul>
19+
20+
<p>机器人按顺序执行指令&nbsp;<code>instructions</code>,并一直重复它们。</p>
21+
22+
<p>只有在平面中存在环使得机器人永远无法离开时,返回&nbsp;<code>true</code>。否则,返回 <code>false</code>。</p>
23+
24+
<p>&nbsp;</p>
25+
26+
<p><strong>示例 1:</strong></p>
27+
28+
<pre>
29+
<strong>输入:</strong>instructions = "GGLLGG"
30+
<strong>输出:</strong>true
31+
<strong>解释:</strong>机器人最初在(0,0)处,面向北方。
32+
“G”:移动一步。位置:(0,1)方向:北。
33+
“G”:移动一步。位置:(0,2).方向:北。
34+
“L”:逆时针旋转90度。位置:(0,2).方向:西。
35+
“L”:逆时针旋转90度。位置:(0,2)方向:南。
36+
“G”:移动一步。位置:(0,1)方向:南。
37+
“G”:移动一步。位置:(0,0)方向:南。
38+
重复指令,机器人进入循环:(0,0)——&gt;(0,1)——&gt;(0,2)——&gt;(0,1)——&gt;(0,0)。
39+
在此基础上,我们返回true。
40+
</pre>
41+
42+
<p><strong>示例 2:</strong></p>
43+
44+
<pre>
45+
<strong>输入:</strong>instructions = "GG"
46+
<strong>输出:</strong>false
47+
<strong>解释:</strong>机器人最初在(0,0)处,面向北方。
48+
“G”:移动一步。位置:(0,1)方向:北。
49+
“G”:移动一步。位置:(0,2).方向:北。
50+
重复这些指示,继续朝北前进,不会进入循环。
51+
在此基础上,返回false。
52+
</pre>
53+
54+
<p><strong>示例 3:</strong></p>
55+
56+
<pre>
57+
<strong>输入:</strong>instructions = "GL"
58+
<strong>输出:</strong>true
59+
<strong>解释:</strong>机器人最初在(0,0)处,面向北方。
60+
“G”:移动一步。位置:(0,1)方向:北。
61+
“L”:逆时针旋转90度。位置:(0,1).方向:西。
62+
“G”:移动一步。位置:(- 1,1)方向:西。
63+
“L”:逆时针旋转90度。位置:(- 1,1)方向:南。
64+
“G”:移动一步。位置:(- 1,0)方向:南。
65+
“L”:逆时针旋转90度。位置:(- 1,0)方向:东方。
66+
“G”:移动一步。位置:(0,0)方向:东方。
67+
“L”:逆时针旋转90度。位置:(0,0)方向:北。
68+
重复指令,机器人进入循环:(0,0)——&gt;(0,1)——&gt;(- 1,1)——&gt;(- 1,0)——&gt;(0,0)。
69+
在此基础上,我们返回true。</pre>
70+
71+
<p>&nbsp;</p>
72+
73+
<p><strong>提示:</strong></p>
74+
75+
<ul>
76+
<li><code>1 &lt;= instructions.length &lt;= 100</code></li>
77+
<li><code>instructions[i]</code>&nbsp;仅包含&nbsp;<code>'G', 'L', 'R'</code></li>
78+
</ul>

problems/problems_105/Solution.cpp

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,20 @@ class Solution {
4545
};
4646

4747
json leetcode::qubh::Solve(string input_json_values) {
48-
vector<string> inputArray;
49-
size_t pos = input_json_values.find('\n');
50-
while (pos != string::npos) {
51-
inputArray.push_back(input_json_values.substr(0, pos));
52-
input_json_values = input_json_values.substr(pos + 1);
53-
pos = input_json_values.find('\n');
54-
}
55-
inputArray.push_back(input_json_values);
48+
vector<string> inputArray;
49+
size_t pos = input_json_values.find('\n');
50+
while (pos != string::npos) {
51+
inputArray.push_back(input_json_values.substr(0, pos));
52+
input_json_values = input_json_values.substr(pos + 1);
53+
pos = input_json_values.find('\n');
54+
}
55+
inputArray.push_back(input_json_values);
5656

57-
Solution solution;
58-
vector<int> preorder = json::parse(inputArray.at(0));
59-
vector<int> inorder = json::parse(inputArray.at(1));
60-
return TreeNodeToJsonArray(solution.buildTree(preorder, inorder));
57+
Solution solution;
58+
vector<int> preorder = json::parse(inputArray.at(0));
59+
vector<int> inorder = json::parse(inputArray.at(1));
60+
TreeNode *res_ptr = solution.buildTree(preorder, inorder);
61+
json final_ans = TreeNodeToJsonArray(res_ptr);
62+
delete res_ptr;
63+
return final_ans;
6164
}

0 commit comments

Comments
 (0)