-
Notifications
You must be signed in to change notification settings - Fork 13
feat: cpp memory improve #160
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a904992
fix: c++ memory leaks
QuBenhao f9134fa
fix: c++ ListNode free
QuBenhao e60a2c5
fix: c++ tree memory
QuBenhao 7847b3a
test: LCR_055 cpp memory
QuBenhao 6b80296
test: add dev problem 2
QuBenhao 67e8310
feat: cpp node neightbor
QuBenhao File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
{ | ||
"daily": "2767", | ||
"daily": "1932", | ||
"plans": ["1", "problems", "LCR_115", "problems"] | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// go:build ignore | ||
#include <queue> | ||
|
||
#include "cpp/common/Solution.h" | ||
#include "cpp/models/NodeNeighbors.h" | ||
|
||
using namespace std; | ||
using json = nlohmann::json; | ||
|
||
/* | ||
// Definition for a Node. | ||
class Node { | ||
public: | ||
int val; | ||
vector<Node*> neighbors; | ||
Node() { | ||
val = 0; | ||
neighbors = vector<Node*>(); | ||
} | ||
Node(int _val) { | ||
val = _val; | ||
neighbors = vector<Node*>(); | ||
} | ||
Node(int _val, vector<Node*> _neighbors) { | ||
val = _val; | ||
neighbors = _neighbors; | ||
} | ||
}; | ||
*/ | ||
|
||
class Solution { | ||
public: | ||
Node *cloneGraph(Node *node) { | ||
if (node == nullptr) { | ||
return node; | ||
} | ||
|
||
unordered_map<Node *, Node *> visited; | ||
|
||
// 将题目给定的节点添加到队列 | ||
queue<Node *> Q; | ||
Q.push(node); | ||
// 克隆第一个节点并存储到哈希表中 | ||
visited[node] = new Node(node->val); | ||
|
||
// 广度优先搜索 | ||
while (!Q.empty()) { | ||
// 取出队列的头节点 | ||
auto n = Q.front(); | ||
Q.pop(); | ||
// 遍历该节点的邻居 | ||
for (auto &neighbor : n->neighbors) { | ||
if (visited.find(neighbor) == visited.end()) { | ||
// 如果没有被访问过,就克隆并存储在哈希表中 | ||
visited[neighbor] = new Node(neighbor->val); | ||
// 将邻居节点加入队列中 | ||
Q.push(neighbor); | ||
} | ||
// 更新当前节点的邻居列表 | ||
visited[n]->neighbors.emplace_back(visited[neighbor]); | ||
} | ||
} | ||
|
||
return visited[node]; | ||
} | ||
}; | ||
|
||
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); | ||
|
||
Solution solution; | ||
vector<vector<int>> node_arrays = json::parse(inputArray.at(0)); | ||
Node *node = JsonArrayToNodeNeighbors(node_arrays); | ||
Node *res_ptr = solution.cloneGraph(node); | ||
json final_ans = NodeNeighborsToJsonArray(res_ptr); | ||
DeleteGraph(node); // Delete the graph to prevent memory leak | ||
DeleteGraph(res_ptr); // Delete the graph to prevent memory leak | ||
return final_ans; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package problems.problems_133; | ||
|
||
import com.alibaba.fastjson.JSON; | ||
import java.util.*; | ||
import qubhjava.BaseSolution; | ||
/* | ||
// Definition for a Node. | ||
class Node { | ||
public int val; | ||
public List<Node> neighbors; | ||
public Node() { | ||
val = 0; | ||
neighbors = new ArrayList<Node>(); | ||
} | ||
public Node(int _val) { | ||
val = _val; | ||
neighbors = new ArrayList<Node>(); | ||
} | ||
public Node(int _val, ArrayList<Node> _neighbors) { | ||
val = _val; | ||
neighbors = _neighbors; | ||
} | ||
} | ||
*/ | ||
|
||
import qubhjava.models.node.neighbors.Node; | ||
|
||
public class Solution extends BaseSolution { | ||
public Node cloneGraph(Node node) { | ||
|
||
} | ||
|
||
@Override | ||
public Object solve(String[] inputJsonValues) { | ||
Node node = Node.ArrayToNodeNeighbors(jsonArrayToInt2DArray(inputJsonValues[0])); | ||
return JSON.toJSON(Node.NodeNeighborsToArray(cloneGraph(node))); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.