Skip to content

111 complex problems #114

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 46 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
170a06f
feat: language_writer.py
QuBenhao Jul 25, 2024
caab4f7
test: add special codes
QuBenhao Jul 25, 2024
28a0bc7
test: add 236 testcase
QuBenhao Jul 26, 2024
f1b863d
feat: python list_to_tree_with_target
QuBenhao Jul 26, 2024
4a5c3ce
test: add 863, 1379 testcase
QuBenhao Jul 26, 2024
a33198e
fix: 1379 cloned tree
QuBenhao Jul 26, 2024
3b550f4
feat: add 1379 golang code
QuBenhao Jul 26, 2024
7b4a4a6
feat: golang writer tree with targets
QuBenhao Jul 26, 2024
da01523
fix: golang writer tree with targets
QuBenhao Jul 27, 2024
162187a
feat: cpp writer tree with targets
QuBenhao Jul 27, 2024
1399844
test: 236 cpp writer tree with targets
QuBenhao Jul 27, 2024
7bd0a6a
feat: java writer tree with targets
QuBenhao Jul 27, 2024
f491e7e
feat: typescript writer tree with targets
QuBenhao Jul 27, 2024
05411b4
feat: rust writer tree with targets
QuBenhao Jul 27, 2024
bf4e832
feat: python writer support Node with next
QuBenhao Jul 27, 2024
52acf48
Merge branch 'refs/heads/master' into 111-complex-problems
QuBenhao Jul 27, 2024
fb56c48
refactor: rename golang TreeNodeNext
QuBenhao Jul 28, 2024
aa758cb
feat: c++ treeNode with next
QuBenhao Jul 28, 2024
878580b
feat: java treeNode with next
QuBenhao Jul 28, 2024
dc03e06
feat: typescript treeNode with next
QuBenhao Jul 28, 2024
59cdbd5
feat: rust treeNode with next
QuBenhao Jul 28, 2024
767726a
feat: c++ node with neighbors
QuBenhao Jul 28, 2024
c3086c5
feat: Java node with neighbors
QuBenhao Jul 29, 2024
17487eb
fix: typescript treeNode target null
Jul 29, 2024
151a26a
feat: Typescript node with neighbors
QuBenhao Jul 29, 2024
8a2727d
feat: rust node with neighbors
QuBenhao Jul 29, 2024
40ec2ee
feat: rust node with neighbors
QuBenhao Jul 29, 2024
eaf9127
feat: rust node with neighbors
QuBenhao Jul 29, 2024
ef9509c
test: rust 133
QuBenhao Jul 29, 2024
cb07b4f
feat: c++ node with neighbors
QuBenhao Jul 29, 2024
438ea80
feat: c++ node with random
QuBenhao Jul 30, 2024
3a641f6
fix
Jul 30, 2024
099705a
feat: Java node with random
QuBenhao Jul 30, 2024
37122d9
feat: Typescript node with random
QuBenhao Jul 30, 2024
336f286
feat: rust node with random
QuBenhao Jul 30, 2024
ca53dde
Merge branch 'refs/heads/master' into 111-complex-problems
QuBenhao Jul 30, 2024
88ae85f
feat: python add ListNode with random
QuBenhao Jul 31, 2024
2dfd4f6
feat: python ListNode with Cycle or intersection
QuBenhao Jul 31, 2024
f429efd
feat: golang ListNode with Cycle or intersection
QuBenhao Jul 31, 2024
e90178f
feat: golang ListNode with Cycle or intersection
QuBenhao Jul 31, 2024
9585f18
feat: java ListNode with Cycle or intersection
QuBenhao Jul 31, 2024
07fda7a
feat: c++ ListNode with Cycle or intersection
QuBenhao Jul 31, 2024
153764c
feat: c++ ListNode with Cycle or intersection
QuBenhao Jul 31, 2024
d1fd2eb
feat: typescript ListNode with Cycle or intersection
QuBenhao Jul 31, 2024
eb02f73
Merge branch 'refs/heads/master' into 111-complex-problems
QuBenhao Jul 31, 2024
12f2ddc
Merge branch 'master' into 111-complex-problems
QuBenhao Jul 31, 2024
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
66 changes: 66 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ members = [
"problems/problems_3106",
"problems/problems_34",
"problems/problems_102",
"problems/problems_1379",
"problems/problems_236",
"problems/problems_116",
"problems/problems_117",
"problems/problems_133",
"problems/problems_138",
"problems/problems_699",
"problems/problems_39",
"problems/problems_215",
Expand Down Expand Up @@ -60,6 +66,12 @@ solution_239 = { path = "problems/problems_239", features = ["solution_239"] }
solution_3106 = { path = "problems/problems_3106", features = ["solution_3106"] }
solution_34 = { path = "problems/problems_34", features = ["solution_34"] }
solution_102 = { path = "problems/problems_102", features = ["solution_102"] }
solution_1379 = { path = "problems/problems_1379", features = ["solution_1379"] }
solution_236 = { path = "problems/problems_236", features = ["solution_236"] }
solution_116 = { path = "problems/problems_116", features = ["solution_116"] }
solution_117 = { path = "problems/problems_117", features = ["solution_117"] }
solution_133 = { path = "problems/problems_133", features = ["solution_133"] }
solution_138 = { path = "problems/problems_138", features = ["solution_138"] }
solution_699 = { path = "problems/problems_699", features = ["solution_699"] }
solution_39 = { path = "problems/problems_39", features = ["solution_39"] }
solution_215 = { path = "problems/problems_215", features = ["solution_215"] }
Expand Down
13 changes: 11 additions & 2 deletions cpp/models/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,20 @@ cc_library(
name = "models",
srcs = [
"ListNode.cpp",
"NodeNeighbors.cpp",
"NodeRandom.cpp",
"TreeNode.cpp",
"TreeNodeNext.cpp",
],
hdrs = ["ListNode.h", "TreeNode.h"],
hdrs = [
"ListNode.h",
"NodeNeighbors.h",
"NodeRandom.h",
"TreeNode.h",
"TreeNodeNext.h",
],
visibility = ["//visibility:public"],
deps = [
"@nlohmann_json//:json",
],
visibility = ["//visibility:public"],
)
10 changes: 5 additions & 5 deletions cpp/models/ListNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,18 @@ ListNode *IntArrayToListNodeCycle(std::vector<int> &arr, int pos) {
}

std::tuple<ListNode *, ListNode *>
IntArrayToIntersectionListNode(std::vector<int> &arr1, std::vector<int> &arr2, int iv, int idxA, int idxB) {
IntArrayToIntersectionListNode(int iv, std::vector<int> &arr1, std::vector<int> &arr2, int idx_a, int idx_b) {
auto headA = IntArrayToListNode(arr1);
if (iv == 0 || idxA == static_cast<int>(arr1.size()) || idxB == static_cast<int>(arr2.size())) {
if (iv == 0 || idx_a == static_cast<int>(arr1.size()) || idx_b == static_cast<int>(arr2.size())) {
return {headA, IntArrayToListNode(arr2)};
}
auto pa = headA;
for (int i = 0; i < idxA; i++) {
for (int i = 0; i < idx_a; i++) {
pa = pa->next;
}
auto headB = idxB == 0 ? pa : new ListNode(arr2[0]);
auto headB = idx_b == 0 ? pa : new ListNode(arr2[0]);
auto pb = headB;
for (int i = 1; i < idxB - 1; i++) {
for (int i = 1; i < idx_b - 1; i++) {
pb->next = new ListNode(arr2[i]);
pb = pb->next;
}
Expand Down
2 changes: 1 addition & 1 deletion cpp/models/ListNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ std::vector<int> &ListNodeToIntArray(ListNode *head);
ListNode *IntArrayToListNodeCycle(std::vector<int> &arr, int pos);

std::tuple<ListNode *, ListNode *>
IntArrayToIntersectionListNode(std::vector<int> &arr1, std::vector<int> &arr2, int iv, int idxA, int idxB);
IntArrayToIntersectionListNode(int iv, std::vector<int> &arr1, std::vector<int> &arr2, int idx_a, int idx_b);

#endif //LEETCODECPP_LISTNODE_H
54 changes: 54 additions & 0 deletions cpp/models/NodeNeighbors.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//
// Created by BenHao on 2024/7/29.
//

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

Node *JsonArrayToNodeNeighbors(vector<vector<int>> arr) {
if (arr.empty()) {
return nullptr;
}
vector<Node *> nodes = vector<Node *>(arr.size() + 1);
for (size_t i = 1; i <= arr.size(); i++) {
nodes[i] = new Node(static_cast<int>(i));
}
for (size_t i = 0; i < arr.size(); i++) {
for (size_t j = 0; j < arr[i].size(); j++) {
nodes[i + 1]->neighbors.emplace_back(nodes[arr[i][j]]);
}
}
return nodes[1];
}

void dfs(Node* cur, vector<vector<int>>& ans, std::unordered_set<int>& visited) {
if (ans.size() < cur->val) {
for (auto i = ans.size(); i < cur->val; i++) {
ans.emplace_back();
}
for (auto neighbor : cur->neighbors) {
ans.back().emplace_back(neighbor->val);
}
} else {
for (auto neighbor : cur->neighbors) {
ans[cur->val - 1].emplace_back(neighbor->val);
}
}
for (auto neighbor : cur->neighbors) {
if (visited.find(neighbor->val) == visited.end()) {
visited.insert(neighbor->val);
dfs(neighbor, ans, visited);
}
}
}

vector<vector<int>> NodeNeighborsToJsonArray(Node *root) {
vector<vector<int>> ans;
if (root == nullptr) {
return ans;
}
std::unordered_set<int> visited;
visited.insert(root->val);
dfs(root, ans, visited);
return ans;
}
40 changes: 40 additions & 0 deletions cpp/models/NodeNeighbors.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// Created by BenHao on 2024/7/29.
//

#ifndef LEETCODE_NODENEIGHBORS_H
#define LEETCODE_NODENEIGHBORS_H

#include <utility>
#include <vector>
#include <nlohmann/json.hpp>

using json = nlohmann::json;
using std::vector;

// Definition for a Node.
class Node {
public:
int val;
vector<Node *> neighbors;

Node() {
val = 0;
neighbors = vector<Node *>();
}

explicit Node(int _val) {
val = _val;
neighbors = vector<Node *>();
}

Node(int _val, vector<Node *> _neighbors) {
val = _val;
neighbors = std::move(_neighbors);
}
};

Node *JsonArrayToNodeNeighbors(vector<vector<int>> arr);
vector<vector<int>> NodeNeighborsToJsonArray(Node *root);

#endif //LEETCODE_NODENEIGHBORS_H
52 changes: 52 additions & 0 deletions cpp/models/NodeRandom.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//
// Created by BenHao on 2024/7/29.
//

#include "NodeRandom.h"
#include <unordered_map>

Node* JsonArrayToNodeRandom(json arr) {
if (arr.empty()) {
return nullptr;
}
vector<Node*> nodes = vector<Node*>(arr.size());
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
nodes[i] = new Node(0);
}
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
auto node = nodes[i];
node->val = arr[i][0];
if (i < static_cast<int>(arr.size()) - 1) {
node->next = nodes[i + 1];
}
if (!arr[i][1].is_null()) {
node->random = nodes[arr[i][1]];
}
}
return nodes[0];
}

json NodeRandomToJsonArray(Node* head) {
json arr = json::array();
std::unordered_map<Node*, int> nodeMap;
int index = 0;
Node* node = head;
while (node != nullptr) {
nodeMap[node] = index;
node = node->next;
index++;
}
node = head;
while (node != nullptr) {
json item = json::array();
item.push_back(node->val);
if (node->random != nullptr) {
item.push_back(nodeMap[node->random]);
} else {
item.push_back(nullptr);
}
arr.push_back(item);
node = node->next;
}
return arr;
}
31 changes: 31 additions & 0 deletions cpp/models/NodeRandom.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// Created by BenHao on 2024/7/29.
//

#ifndef LEETCODE_NODERANDOM_H
#define LEETCODE_NODERANDOM_H

#include "nlohmann/json.hpp"
#include <vector>

using std::vector;
using nlohmann::json;

// Definition for a Node.
class Node {
public:
int val;
Node* next;
Node* random;

explicit Node(int _val) {
val = _val;
next = nullptr;
random = nullptr;
}
};

Node* JsonArrayToNodeRandom(json arr);
json NodeRandomToJsonArray(Node* head);

#endif //LEETCODE_NODERANDOM_H
2 changes: 1 addition & 1 deletion cpp/models/TreeNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ TreeNode *JsonArrayToTreeNode(json arr) {

vector<TreeNode *> JsonArrayToTreeNodeWithTargets(json arr, vector<int> targets) {
if (arr.empty()) {
return vector<TreeNode *>(targets.size() + 1, nullptr);
return {targets.size() + 1, nullptr};
}
vector<TreeNode *> ans = vector<TreeNode *>(targets.size() + 1);
auto root = new TreeNode(arr[0]);
Expand Down
Loading