Skip to content

Merge from master #73

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 41 commits into from
Jun 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
340904a
feat: cpp memory improve (#160)
QuBenhao Jun 12, 2025
5ae865f
10.1.0
Jun 12, 2025
2537ee8
test: 943 solution
QuBenhao Jun 12, 2025
040fbe6
test: [20250613] Add (2616)
actions-user Jun 12, 2025
5de4938
test: 2616 solution
QuBenhao Jun 12, 2025
5e910f7
fix: leetcode force get problem
QuBenhao Jun 12, 2025
82d5262
test: 943 solution
QuBenhao Jun 12, 2025
9f11ef3
fix: line break
QuBenhao Jun 13, 2025
80c7d19
test: 2125 solution
QuBenhao Jun 13, 2025
022b4f3
fix: contest template
QuBenhao Jun 13, 2025
d920ff7
test: 3531 solution
QuBenhao Jun 13, 2025
d4c75ad
test: 3532 solution
QuBenhao Jun 13, 2025
e7c9319
test: 3533 timeout
QuBenhao Jun 13, 2025
769c6bb
test: 3533 solution
QuBenhao Jun 13, 2025
aa1503f
test: 3533 solution
QuBenhao Jun 13, 2025
cf38bdc
test: [20250614] Add (2566)
actions-user Jun 13, 2025
90ec84c
test: 2566 solution
QuBenhao Jun 13, 2025
2afcabf
test: 1642 solution
QuBenhao Jun 13, 2025
c3ed36d
test: 1483 solution
QuBenhao Jun 13, 2025
75d2246
test: 3534 solution
QuBenhao Jun 14, 2025
477175f
test: 3553 solution
QuBenhao Jun 14, 2025
1da56e7
test: 2103 solution
QuBenhao Jun 14, 2025
3c4ad5a
test: [20250615] Add (1432)
actions-user Jun 14, 2025
79bfa94
test: 1432 solution
QuBenhao Jun 14, 2025
3e980a6
test: 1504 solution
QuBenhao Jun 15, 2025
6a5e57b
test: 1504 solution
QuBenhao Jun 15, 2025
c7d112d
test: 316 solution
QuBenhao Jun 15, 2025
9774a03
test: 3582, 3583, 3584, 3585 solution
QuBenhao Jun 15, 2025
aca2ac6
test: 3582, 3583, 3584, 3585 solution
QuBenhao Jun 15, 2025
604013c
test: 3582, 3583, 3584, 3585 solution
QuBenhao Jun 15, 2025
7b202d8
test: 3582, 3583, 3584, 3585 solution
QuBenhao Jun 15, 2025
22c7f95
test: 1732 solution
QuBenhao Jun 15, 2025
a553d8f
test: [20250616] Add (2016)
actions-user Jun 15, 2025
96c0083
test: 2016 solution
QuBenhao Jun 15, 2025
bc2ce0e
test: add 321
QuBenhao Jun 15, 2025
ae709b8
test: 321 solution
QuBenhao Jun 16, 2025
5d0057a
test: 321
QuBenhao Jun 16, 2025
bb5ad88
test: 402 solution
QuBenhao Jun 16, 2025
2400f65
test: go build ignore
QuBenhao Jun 16, 2025
70b8af3
test: 1206 solution
QuBenhao Jun 16, 2025
404023d
test: 1206 solution
QuBenhao Jun 16, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 46 additions & 16 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,41 @@
# CHANGELOG

## v10.1.0 (2025-06-12)

### Feature

* feat: cpp memory improve (#160)

* fix: c++ memory leaks

prevent memory leaks and improve memory management in ListNode and Node classes

* fix: c++ ListNode free

improve memory management in ListNode handling and prevent leaks

* fix: c++ tree memory

update daily problem ID and enhance memory management in solutions

* test: LCR_055 cpp memory

delete root

* test: add dev problem 2

dev problem 2

* feat: cpp node neightbor

implement graph cloning and memory management functions ([`340904a`](https://github.com/QuBenhao/LeetCode/commit/340904a506bc591acda97dd10bfc489b660cbd6e))

### Test

* test: 3574 solution

py, c++, go ([`b647870`](https://github.com/QuBenhao/LeetCode/commit/b64787041b5213966c3711e645a71c6ffd75b37c))

## v10.0.2 (2025-06-12)

### Fix
Expand Down Expand Up @@ -100,22 +136,6 @@ folder after plan problem

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

### Test

* test: 3445 solution

c++, go, java ([`ad2c53d`](https://github.com/QuBenhao/LeetCode/commit/ad2c53d4c4f0b14c763f264a91611f4645c8b07d))

* test: 3445 solution

py ([`6deb184`](https://github.com/QuBenhao/LeetCode/commit/6deb184396515bb0d926b01f01af41cfec2c3c11))

* test: [20250611] Add (3445) ([`e76129d`](https://github.com/QuBenhao/LeetCode/commit/e76129d6ea562d47323f22ef276ed7ee82914025))

## v9.8.0 (2025-06-10)

### Breaking

* feat: leetcode script

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

### Test

* test: 3445 solution

c++, go, java ([`ad2c53d`](https://github.com/QuBenhao/LeetCode/commit/ad2c53d4c4f0b14c763f264a91611f4645c8b07d))

* test: 3445 solution

py ([`6deb184`](https://github.com/QuBenhao/LeetCode/commit/6deb184396515bb0d926b01f01af41cfec2c3c11))

* test: [20250611] Add (3445) ([`e76129d`](https://github.com/QuBenhao/LeetCode/commit/e76129d6ea562d47323f22ef276ed7ee82914025))

* test: 3442 solution

py, c++, go, java ([`2e1fbe3`](https://github.com/QuBenhao/LeetCode/commit/2e1fbe3b583e9b187af03306055c01c03dc0cee6))
Expand Down
8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,10 @@ members = [
"problems/problems_3442",
"problems/problems_3445",
"problems/problems_3423",
"problems/problems_2616",
"problems/problems_2566",
"problems/problems_1432",
"problems/problems_2016",
]

[package]
Expand Down Expand Up @@ -718,3 +722,7 @@ solution_440 = { path = "problems/problems_440", features = ["solution_440"] }
solution_3442 = { path = "problems/problems_3442", features = ["solution_3442"] }
solution_3445 = { path = "problems/problems_3445", features = ["solution_3445"] }
solution_3423 = { path = "problems/problems_3423", features = ["solution_3423"] }
solution_2616 = { path = "problems/problems_2616", features = ["solution_2616"] }
solution_2566 = { path = "problems/problems_2566", features = ["solution_2566"] }
solution_1432 = { path = "problems/problems_1432", features = ["solution_1432"] }
solution_2016 = { path = "problems/problems_2016", features = ["solution_2016"] }
26 changes: 16 additions & 10 deletions cpp/models/ListNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,42 @@

#include "ListNode.h"

ListNode *IntArrayToListNode(std::vector<int> &arr) {
auto dummy = new ListNode(), p = dummy;
ListNode *IntArrayToListNode(const std::vector<int> &arr) {
ListNode dummy = ListNode();
ListNode *p = &dummy;
for (auto val : arr) {
p->next = new ListNode(val);
p = p->next;
}
return dummy->next;
p = dummy.next;
dummy.next = nullptr; // Prevent memory leak
return p;
}

std::vector<int> &ListNodeToIntArray(ListNode *head) {
auto *arr = new std::vector<int>();
std::vector<int> ListNodeToIntArray(ListNode *head) {
auto arr = std::vector<int>();
while (head != nullptr) {
arr->push_back(head->val);
arr.push_back(head->val);
head = head->next;
}
return *arr;
return arr;
}

ListNode *IntArrayToListNodeCycle(std::vector<int> &arr, int pos) {
auto dummy = new ListNode(), p = dummy;
ListNode dummy = ListNode();
ListNode *p = &dummy;
ListNode *cycle = nullptr;
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
for (size_t i = 0; i < arr.size(); ++i) {
p->next = new ListNode(arr[i]);
p = p->next;
if (i == pos) {
cycle = p;
}
}
p->next = cycle;
return dummy->next;
p = dummy.next;
dummy.next = nullptr; // Prevent memory leak
return p;
}

std::tuple<ListNode *, ListNode *>
Expand Down
4 changes: 2 additions & 2 deletions cpp/models/ListNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ struct ListNode {
}
};

ListNode *IntArrayToListNode(std::vector<int> &arr);
ListNode *IntArrayToListNode(const std::vector<int> &arr);

std::vector<int> &ListNodeToIntArray(ListNode *head);
std::vector<int> ListNodeToIntArray(ListNode *head);

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

Expand Down
24 changes: 22 additions & 2 deletions cpp/models/NodeNeighbors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

#include "NodeNeighbors.h"
#include <unordered_set>
#include <queue>

Node *JsonArrayToNodeNeighbors(vector<vector<int>> arr) {
Node *JsonArrayToNodeNeighbors(const vector<vector<int>>& arr) {
if (arr.empty()) {
return nullptr;
}
vector<Node *> nodes = vector<Node *>(arr.size() + 1);
vector<Node *> nodes = vector<Node *>(arr.size() + 1, nullptr);
for (size_t i = 1; i <= arr.size(); i++) {
nodes[i] = new Node(static_cast<int>(i));
}
Expand Down Expand Up @@ -52,4 +53,23 @@ vector<vector<int>> NodeNeighborsToJsonArray(Node *root) {
visited.insert(root->val);
dfs(root, ans, visited);
return ans;
}

void DeleteGraph(Node* root) {
if (!root) return;
std::unordered_set<Node*> visited;
std::queue<Node*> q;
q.push(root);
visited.insert(root);
while (!q.empty()) {
Node* node = q.front();
q.pop();
for (Node* neighbor : node->neighbors) {
if (neighbor && !visited.count(neighbor)) {
visited.insert(neighbor);
q.push(neighbor);
}
}
delete node;
}
}
3 changes: 2 additions & 1 deletion cpp/models/NodeNeighbors.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ class Node {
}
};

Node *JsonArrayToNodeNeighbors(vector<vector<int>> arr);
Node *JsonArrayToNodeNeighbors(const vector<vector<int>>& arr);
vector<vector<int>> NodeNeighborsToJsonArray(Node *root);
void DeleteGraph(Node* root);

#endif //LEETCODE_NODENEIGHBORS_H
4 changes: 4 additions & 0 deletions cpp/models/NodeRandom.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ class Node {
next = nullptr;
random = nullptr;
}

~Node() {
delete next; // Automatically delete the next node to avoid memory leaks
}
};

Node* JsonArrayToNodeRandom(json arr);
Expand Down
6 changes: 6 additions & 0 deletions cpp/models/TreeNodeNext.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ class Node {

Node(int _val, Node* _left, Node* _right, Node* _next)
: val(_val), left(_left), right(_right), next(_next) {}

~Node() {
delete left;
delete right;
// next is not deleted here because it may point to another Node in the same tree
}
};

Node *JsonArrayToTreeNodeNext(json arr);
Expand Down
4 changes: 2 additions & 2 deletions daily-problems.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"daily": "2767",
"plans": ["1", "problems", "LCR_115", "problems"]
"daily": "1206",
"plans": ["3582", "problems", "3583", "problems", "3584", "problems", "3585", "problems"]
}
10 changes: 8 additions & 2 deletions golang/problems_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package golang

import (
"leetCode/problems/problems_LCR_115"
"leetCode/problems/problems_3582"
"leetCode/problems/problems_3583"
"leetCode/problems/problems_3584"
"leetCode/problems/problems_3585"
"testing"
)

func TestSolutions(t *testing.T) {
TestEach(t, "LCR_115", "problems", problemLCR_115.Solve)
TestEach(t, "3582", "problems", problem3582.Solve)
TestEach(t, "3583", "problems", problem3583.Solve)
TestEach(t, "3584", "problems", problem3584.Solve)
TestEach(t, "3585", "problems", problem3585.Solve)
}
4 changes: 2 additions & 2 deletions golang/solution_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package golang

import (
problem "leetCode/problems/problems_2767"
problem "leetCode/problems/problems_1206"
"testing"
)

func TestSolution(t *testing.T) {
TestEach(t, "2767", "problems", problem.Solve)
TestEach(t, "1206", "problems", problem.Solve)
}
78 changes: 78 additions & 0 deletions problems/problems_1041/problem_zh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# 1041. 困于环中的机器人 [难度分: 1521.18]

<p>在无限的平面上,机器人最初位于&nbsp;<code>(0, 0)</code>&nbsp;处,面朝北方。注意:</p>

<ul>
<li><strong>北方向</strong> 是y轴的正方向。</li>
<li><strong>南方向</strong> 是y轴的负方向。</li>
<li><strong>东方向</strong> 是x轴的正方向。</li>
<li><strong>西方向</strong> 是x轴的负方向。</li>
</ul>

<p>机器人可以接受下列三条指令之一:</p>

<ul>
<li><code>"G"</code>:直走 1 个单位</li>
<li><code>"L"</code>:左转 90 度</li>
<li><code>"R"</code>:右转 90 度</li>
</ul>

<p>机器人按顺序执行指令&nbsp;<code>instructions</code>,并一直重复它们。</p>

<p>只有在平面中存在环使得机器人永远无法离开时,返回&nbsp;<code>true</code>。否则,返回 <code>false</code>。</p>

<p>&nbsp;</p>

<p><strong>示例 1:</strong></p>

<pre>
<strong>输入:</strong>instructions = "GGLLGG"
<strong>输出:</strong>true
<strong>解释:</strong>机器人最初在(0,0)处,面向北方。
“G”:移动一步。位置:(0,1)方向:北。
“G”:移动一步。位置:(0,2).方向:北。
“L”:逆时针旋转90度。位置:(0,2).方向:西。
“L”:逆时针旋转90度。位置:(0,2)方向:南。
“G”:移动一步。位置:(0,1)方向:南。
“G”:移动一步。位置:(0,0)方向:南。
重复指令,机器人进入循环:(0,0)——&gt;(0,1)——&gt;(0,2)——&gt;(0,1)——&gt;(0,0)。
在此基础上,我们返回true。
</pre>

<p><strong>示例 2:</strong></p>

<pre>
<strong>输入:</strong>instructions = "GG"
<strong>输出:</strong>false
<strong>解释:</strong>机器人最初在(0,0)处,面向北方。
“G”:移动一步。位置:(0,1)方向:北。
“G”:移动一步。位置:(0,2).方向:北。
重复这些指示,继续朝北前进,不会进入循环。
在此基础上,返回false。
</pre>

<p><strong>示例 3:</strong></p>

<pre>
<strong>输入:</strong>instructions = "GL"
<strong>输出:</strong>true
<strong>解释:</strong>机器人最初在(0,0)处,面向北方。
“G”:移动一步。位置:(0,1)方向:北。
“L”:逆时针旋转90度。位置:(0,1).方向:西。
“G”:移动一步。位置:(- 1,1)方向:西。
“L”:逆时针旋转90度。位置:(- 1,1)方向:南。
“G”:移动一步。位置:(- 1,0)方向:南。
“L”:逆时针旋转90度。位置:(- 1,0)方向:东方。
“G”:移动一步。位置:(0,0)方向:东方。
“L”:逆时针旋转90度。位置:(0,0)方向:北。
重复指令,机器人进入循环:(0,0)——&gt;(0,1)——&gt;(- 1,1)——&gt;(- 1,0)——&gt;(0,0)。
在此基础上,我们返回true。</pre>

<p>&nbsp;</p>

<p><strong>提示:</strong></p>

<ul>
<li><code>1 &lt;= instructions.length &lt;= 100</code></li>
<li><code>instructions[i]</code>&nbsp;仅包含&nbsp;<code>'G', 'L', 'R'</code></li>
</ul>
27 changes: 15 additions & 12 deletions problems/problems_105/Solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,20 @@ class Solution {
};

json leetcode::qubh::Solve(string input_json_values) {
vector<string> inputArray;
size_t pos = input_json_values.find('\n');
while (pos != string::npos) {
inputArray.push_back(input_json_values.substr(0, pos));
input_json_values = input_json_values.substr(pos + 1);
pos = input_json_values.find('\n');
}
inputArray.push_back(input_json_values);
vector<string> inputArray;
size_t pos = input_json_values.find('\n');
while (pos != string::npos) {
inputArray.push_back(input_json_values.substr(0, pos));
input_json_values = input_json_values.substr(pos + 1);
pos = input_json_values.find('\n');
}
inputArray.push_back(input_json_values);

Solution solution;
vector<int> preorder = json::parse(inputArray.at(0));
vector<int> inorder = json::parse(inputArray.at(1));
return TreeNodeToJsonArray(solution.buildTree(preorder, inorder));
Solution solution;
vector<int> preorder = json::parse(inputArray.at(0));
vector<int> inorder = json::parse(inputArray.at(1));
TreeNode *res_ptr = solution.buildTree(preorder, inorder);
json final_ans = TreeNodeToJsonArray(res_ptr);
delete res_ptr;
return final_ans;
}
Loading