This commit is contained in:
krahets
2023-04-22 01:35:51 +08:00
parent 263c979237
commit 881ece517f
9 changed files with 87 additions and 67 deletions
+2
View File
@@ -1328,6 +1328,8 @@ comments: true
int extendRatio; // 列表每次扩容的倍数 int extendRatio; // 列表每次扩容的倍数
}; };
typedef struct myList myList;
/* 构造函数 */ /* 构造函数 */
myList *newMyList() { myList *newMyList() {
myList *list = malloc(sizeof(myList)); myList *list = malloc(sizeof(myList));
+56 -56
View File
@@ -14,8 +14,8 @@ comments: true
=== "Java" === "Java"
```java title="preorder_find_nodes.java" ```java title="preorder_traversal_i_compact.java"
/* 前序遍历 */ /* 前序遍历:例题一 */
void preOrder(TreeNode root) { void preOrder(TreeNode root) {
if (root == null) { if (root == null) {
return; return;
@@ -31,8 +31,8 @@ comments: true
=== "C++" === "C++"
```cpp title="preorder_find_nodes.cpp" ```cpp title="preorder_traversal_i_compact.cpp"
/* 前序遍历 */ /* 前序遍历:例题一 */
void preOrder(TreeNode *root) { void preOrder(TreeNode *root) {
if (root == nullptr) { if (root == nullptr) {
return; return;
@@ -48,9 +48,9 @@ comments: true
=== "Python" === "Python"
```python title="preorder_find_nodes.py" ```python title="preorder_traversal_i_compact.py"
def pre_order(root: TreeNode) -> None: def pre_order(root: TreeNode) -> None:
"""前序遍历""" """前序遍历:例题一"""
if root is None: if root is None:
return return
if root.val == 7: if root.val == 7:
@@ -62,32 +62,32 @@ comments: true
=== "Go" === "Go"
```go title="preorder_find_nodes.go" ```go title="preorder_traversal_i_compact.go"
[class]{}-[func]{preOrder} [class]{}-[func]{preOrder}
``` ```
=== "JavaScript" === "JavaScript"
```javascript title="preorder_find_nodes.js" ```javascript title="preorder_traversal_i_compact.js"
[class]{}-[func]{preOrder} [class]{}-[func]{preOrder}
``` ```
=== "TypeScript" === "TypeScript"
```typescript title="preorder_find_nodes.ts" ```typescript title="preorder_traversal_i_compact.ts"
[class]{}-[func]{preOrder} [class]{}-[func]{preOrder}
``` ```
=== "C" === "C"
```c title="preorder_find_nodes.c" ```c title="preorder_traversal_i_compact.c"
[class]{}-[func]{preOrder} [class]{}-[func]{preOrder}
``` ```
=== "C#" === "C#"
```csharp title="preorder_find_nodes.cs" ```csharp title="preorder_traversal_i_compact.cs"
/* 前序遍历 */ /* 前序遍历:例题一 */
void preOrder(TreeNode root) void preOrder(TreeNode root)
{ {
if (root == null) if (root == null)
@@ -106,13 +106,13 @@ comments: true
=== "Swift" === "Swift"
```swift title="preorder_find_nodes.swift" ```swift title="preorder_traversal_i_compact.swift"
[class]{}-[func]{preOrder} [class]{}-[func]{preOrder}
``` ```
=== "Zig" === "Zig"
```zig title="preorder_find_nodes.zig" ```zig title="preorder_traversal_i_compact.zig"
[class]{}-[func]{preOrder} [class]{}-[func]{preOrder}
``` ```
@@ -134,8 +134,8 @@ comments: true
=== "Java" === "Java"
```java title="preorder_find_paths.java" ```java title="preorder_traversal_ii_compact.java"
/* 前序遍历 */ /* 前序遍历:例题二 */
void preOrder(TreeNode root) { void preOrder(TreeNode root) {
if (root == null) { if (root == null) {
return; return;
@@ -155,8 +155,8 @@ comments: true
=== "C++" === "C++"
```cpp title="preorder_find_paths.cpp" ```cpp title="preorder_traversal_ii_compact.cpp"
/* 前序遍历 */ /* 前序遍历:例题二 */
void preOrder(TreeNode *root) { void preOrder(TreeNode *root) {
if (root == nullptr) { if (root == nullptr) {
return; return;
@@ -176,9 +176,9 @@ comments: true
=== "Python" === "Python"
```python title="preorder_find_paths.py" ```python title="preorder_traversal_ii_compact.py"
def pre_order(root: TreeNode) -> None: def pre_order(root: TreeNode) -> None:
"""前序遍历""" """前序遍历:例题二"""
if root is None: if root is None:
return return
# 尝试 # 尝试
@@ -194,32 +194,32 @@ comments: true
=== "Go" === "Go"
```go title="preorder_find_paths.go" ```go title="preorder_traversal_ii_compact.go"
[class]{}-[func]{preOrder} [class]{}-[func]{preOrder}
``` ```
=== "JavaScript" === "JavaScript"
```javascript title="preorder_find_paths.js" ```javascript title="preorder_traversal_ii_compact.js"
[class]{}-[func]{preOrder} [class]{}-[func]{preOrder}
``` ```
=== "TypeScript" === "TypeScript"
```typescript title="preorder_find_paths.ts" ```typescript title="preorder_traversal_ii_compact.ts"
[class]{}-[func]{preOrder} [class]{}-[func]{preOrder}
``` ```
=== "C" === "C"
```c title="preorder_find_paths.c" ```c title="preorder_traversal_ii_compact.c"
[class]{}-[func]{preOrder} [class]{}-[func]{preOrder}
``` ```
=== "C#" === "C#"
```csharp title="preorder_find_paths.cs" ```csharp title="preorder_traversal_ii_compact.cs"
/* 前序遍历 */ /* 前序遍历:例题二 */
void preOrder(TreeNode root) void preOrder(TreeNode root)
{ {
if (root == null) if (root == null)
@@ -242,13 +242,13 @@ comments: true
=== "Swift" === "Swift"
```swift title="preorder_find_paths.swift" ```swift title="preorder_traversal_ii_compact.swift"
[class]{}-[func]{preOrder} [class]{}-[func]{preOrder}
``` ```
=== "Zig" === "Zig"
```zig title="preorder_find_paths.zig" ```zig title="preorder_traversal_ii_compact.zig"
[class]{}-[func]{preOrder} [class]{}-[func]{preOrder}
``` ```
@@ -297,8 +297,8 @@ comments: true
=== "Java" === "Java"
```java title="preorder_find_constrained_paths.java" ```java title="preorder_traversal_iii_compact.java"
/* 前序遍历 */ /* 前序遍历:例题三 */
void preOrder(TreeNode root) { void preOrder(TreeNode root) {
// 剪枝 // 剪枝
if (root == null || root.val == 3) { if (root == null || root.val == 3) {
@@ -319,8 +319,8 @@ comments: true
=== "C++" === "C++"
```cpp title="preorder_find_constrained_paths.cpp" ```cpp title="preorder_traversal_iii_compact.cpp"
/* 前序遍历 */ /* 前序遍历:例题三 */
void preOrder(TreeNode *root) { void preOrder(TreeNode *root) {
// 剪枝 // 剪枝
if (root == nullptr || root->val == 3) { if (root == nullptr || root->val == 3) {
@@ -341,9 +341,9 @@ comments: true
=== "Python" === "Python"
```python title="preorder_find_constrained_paths.py" ```python title="preorder_traversal_iii_compact.py"
def pre_order(root: TreeNode) -> None: def pre_order(root: TreeNode) -> None:
"""前序遍历""" """前序遍历:例题三"""
# 剪枝 # 剪枝
if root is None or root.val == 3: if root is None or root.val == 3:
return return
@@ -360,32 +360,32 @@ comments: true
=== "Go" === "Go"
```go title="preorder_find_constrained_paths.go" ```go title="preorder_traversal_iii_compact.go"
[class]{}-[func]{preOrder} [class]{}-[func]{preOrder}
``` ```
=== "JavaScript" === "JavaScript"
```javascript title="preorder_find_constrained_paths.js" ```javascript title="preorder_traversal_iii_compact.js"
[class]{}-[func]{preOrder} [class]{}-[func]{preOrder}
``` ```
=== "TypeScript" === "TypeScript"
```typescript title="preorder_find_constrained_paths.ts" ```typescript title="preorder_traversal_iii_compact.ts"
[class]{}-[func]{preOrder} [class]{}-[func]{preOrder}
``` ```
=== "C" === "C"
```c title="preorder_find_constrained_paths.c" ```c title="preorder_traversal_iii_compact.c"
[class]{}-[func]{preOrder} [class]{}-[func]{preOrder}
``` ```
=== "C#" === "C#"
```csharp title="preorder_find_constrained_paths.cs" ```csharp title="preorder_traversal_iii_compact.cs"
/* 前序遍历 */ /* 前序遍历:例题三 */
void preOrder(TreeNode root) void preOrder(TreeNode root)
{ {
// 剪枝 // 剪枝
@@ -409,13 +409,13 @@ comments: true
=== "Swift" === "Swift"
```swift title="preorder_find_constrained_paths.swift" ```swift title="preorder_traversal_iii_compact.swift"
[class]{}-[func]{preOrder} [class]{}-[func]{preOrder}
``` ```
=== "Zig" === "Zig"
```zig title="preorder_find_constrained_paths.zig" ```zig title="preorder_traversal_iii_compact.zig"
[class]{}-[func]{preOrder} [class]{}-[func]{preOrder}
``` ```
@@ -471,7 +471,7 @@ def backtrack(state, choices, res):
=== "Java" === "Java"
```java title="backtrack_find_constrained_paths.java" ```java title="preorder_traversal_iii_template.java"
/* 判断当前状态是否为解 */ /* 判断当前状态是否为解 */
boolean isSolution(List<TreeNode> state) { boolean isSolution(List<TreeNode> state) {
return !state.isEmpty() && state.get(state.size() - 1).val == 7; return !state.isEmpty() && state.get(state.size() - 1).val == 7;
@@ -497,7 +497,7 @@ def backtrack(state, choices, res):
state.remove(state.size() - 1); state.remove(state.size() - 1);
} }
/* 回溯算法 */ /* 回溯算法:例题三 */
void backtrack(List<TreeNode> state, List<TreeNode> choices, List<List<TreeNode>> res) { void backtrack(List<TreeNode> state, List<TreeNode> choices, List<List<TreeNode>> res) {
// 检查是否为解 // 检查是否为解
if (isSolution(state)) { if (isSolution(state)) {
@@ -521,7 +521,7 @@ def backtrack(state, choices, res):
=== "C++" === "C++"
```cpp title="backtrack_find_constrained_paths.cpp" ```cpp title="preorder_traversal_iii_template.cpp"
/* 判断当前状态是否为解 */ /* 判断当前状态是否为解 */
bool isSolution(vector<TreeNode *> &state) { bool isSolution(vector<TreeNode *> &state) {
return !state.empty() && state.back()->val == 7; return !state.empty() && state.back()->val == 7;
@@ -547,7 +547,7 @@ def backtrack(state, choices, res):
state.pop_back(); state.pop_back();
} }
/* 回溯算法 */ /* 回溯算法:例题三 */
void backtrack(vector<TreeNode *> &state, vector<TreeNode *> &choices, vector<vector<TreeNode *>> &res) { void backtrack(vector<TreeNode *> &state, vector<TreeNode *> &choices, vector<vector<TreeNode *>> &res) {
// 检查是否为解 // 检查是否为解
if (isSolution(state)) { if (isSolution(state)) {
@@ -572,7 +572,7 @@ def backtrack(state, choices, res):
=== "Python" === "Python"
```python title="backtrack_find_constrained_paths.py" ```python title="preorder_traversal_iii_template.py"
def is_solution(state: list[TreeNode]) -> bool: def is_solution(state: list[TreeNode]) -> bool:
"""判断当前状态是否为解""" """判断当前状态是否为解"""
return state and state[-1].val == 7 return state and state[-1].val == 7
@@ -594,7 +594,7 @@ def backtrack(state, choices, res):
state.pop() state.pop()
def backtrack(state: list[TreeNode], choices: list[TreeNode], res: list[list[TreeNode]]): def backtrack(state: list[TreeNode], choices: list[TreeNode], res: list[list[TreeNode]]):
"""回溯算法""" """回溯算法:例题三"""
# 检查是否为解 # 检查是否为解
if is_solution(state): if is_solution(state):
# 记录解 # 记录解
@@ -613,7 +613,7 @@ def backtrack(state, choices, res):
=== "Go" === "Go"
```go title="backtrack_find_constrained_paths.go" ```go title="preorder_traversal_iii_template.go"
[class]{}-[func]{isSolution} [class]{}-[func]{isSolution}
[class]{}-[func]{recordSolution} [class]{}-[func]{recordSolution}
@@ -629,7 +629,7 @@ def backtrack(state, choices, res):
=== "JavaScript" === "JavaScript"
```javascript title="backtrack_find_constrained_paths.js" ```javascript title="preorder_traversal_iii_template.js"
[class]{}-[func]{isSolution} [class]{}-[func]{isSolution}
[class]{}-[func]{recordSolution} [class]{}-[func]{recordSolution}
@@ -645,7 +645,7 @@ def backtrack(state, choices, res):
=== "TypeScript" === "TypeScript"
```typescript title="backtrack_find_constrained_paths.ts" ```typescript title="preorder_traversal_iii_template.ts"
[class]{}-[func]{isSolution} [class]{}-[func]{isSolution}
[class]{}-[func]{recordSolution} [class]{}-[func]{recordSolution}
@@ -661,7 +661,7 @@ def backtrack(state, choices, res):
=== "C" === "C"
```c title="backtrack_find_constrained_paths.c" ```c title="preorder_traversal_iii_template.c"
[class]{}-[func]{isSolution} [class]{}-[func]{isSolution}
[class]{}-[func]{recordSolution} [class]{}-[func]{recordSolution}
@@ -677,7 +677,7 @@ def backtrack(state, choices, res):
=== "C#" === "C#"
```csharp title="backtrack_find_constrained_paths.cs" ```csharp title="preorder_traversal_iii_template.cs"
/* 判断当前状态是否为解 */ /* 判断当前状态是否为解 */
bool isSolution(List<TreeNode> state) bool isSolution(List<TreeNode> state)
{ {
@@ -708,7 +708,7 @@ def backtrack(state, choices, res):
state.RemoveAt(state.Count - 1); state.RemoveAt(state.Count - 1);
} }
/* 回溯算法 */ /* 回溯算法:例题三 */
void backtrack(List<TreeNode> state, List<TreeNode> choices, List<List<TreeNode>> res) void backtrack(List<TreeNode> state, List<TreeNode> choices, List<List<TreeNode>> res)
{ {
// 检查是否为解 // 检查是否为解
@@ -737,7 +737,7 @@ def backtrack(state, choices, res):
=== "Swift" === "Swift"
```swift title="backtrack_find_constrained_paths.swift" ```swift title="preorder_traversal_iii_template.swift"
[class]{}-[func]{isSolution} [class]{}-[func]{isSolution}
[class]{}-[func]{recordSolution} [class]{}-[func]{recordSolution}
@@ -753,7 +753,7 @@ def backtrack(state, choices, res):
=== "Zig" === "Zig"
```zig title="backtrack_find_constrained_paths.zig" ```zig title="preorder_traversal_iii_template.zig"
[class]{}-[func]{isSolution} [class]{}-[func]{isSolution}
[class]{}-[func]{recordSolution} [class]{}-[func]{recordSolution}
@@ -956,6 +956,8 @@ $$
UT_hash_handle hh; // 基于 uthash.h 实现 UT_hash_handle hh; // 基于 uthash.h 实现
}; };
typedef struct hashTable hashTable;
/* 线性阶 */ /* 线性阶 */
void linear(int n) { void linear(int n) {
// 长度为 n 的数组占用 O(n) 空间 // 长度为 n 的数组占用 O(n) 空间
@@ -1006,7 +1008,7 @@ $$
nodes.Add(new ListNode(i)); nodes.Add(new ListNode(i));
} }
// 长度为 n 的哈希表占用 O(n) 空间 // 长度为 n 的哈希表占用 O(n) 空间
Dictionary<int, String> map = new(); Dictionary<int, string> map = new();
for (int i = 0; i < n; i++) for (int i = 0; i < n; i++)
{ {
map.Add(i, i.ToString()); map.Add(i, i.ToString());
+3 -3
View File
@@ -674,7 +674,7 @@ comments: true
Console.Write("顶点列表 = "); Console.Write("顶点列表 = ");
PrintUtil.PrintList(vertices); PrintUtil.PrintList(vertices);
Console.WriteLine("邻接矩阵 ="); Console.WriteLine("邻接矩阵 =");
PrintUtil.printMatrix(adjMat); PrintUtil.PrintMatrix(adjMat);
} }
} }
``` ```
@@ -1382,8 +1382,8 @@ comments: true
{ {
List<int> tmp = new List<int>(); List<int> tmp = new List<int>();
foreach (Vertex vertex in entry.Value) foreach (Vertex vertex in entry.Value)
tmp.Add(vertex.Val); tmp.Add(vertex.val);
Console.WriteLine(entry.Key.Val + ": [" + string.Join(", ", tmp) + "],"); Console.WriteLine(entry.Key.val + ": [" + string.Join(", ", tmp) + "],");
} }
} }
} }
+7 -7
View File
@@ -970,12 +970,12 @@ $$
=== "C#" === "C#"
```csharp title="array_hash_map.cs" ```csharp title="array_hash_map.cs"
/* 键值对 int->String */ /* 键值对 int->string */
class Entry class Entry
{ {
public int key; public int key;
public String val; public string val;
public Entry(int key, String val) public Entry(int key, string val)
{ {
this.key = key; this.key = key;
this.val = val; this.val = val;
@@ -1004,7 +1004,7 @@ $$
} }
/* 查询操作 */ /* 查询操作 */
public String? get(int key) public string? get(int key)
{ {
int index = hashFunc(key); int index = hashFunc(key);
Entry? pair = buckets[index]; Entry? pair = buckets[index];
@@ -1013,7 +1013,7 @@ $$
} }
/* 添加操作 */ /* 添加操作 */
public void put(int key, String val) public void put(int key, string val)
{ {
Entry pair = new Entry(key, val); Entry pair = new Entry(key, val);
int index = hashFunc(key); int index = hashFunc(key);
@@ -1053,9 +1053,9 @@ $$
} }
/* 获取所有值 */ /* 获取所有值 */
public List<String> valueSet() public List<string> valueSet()
{ {
List<String> valueSet = new(); List<string> valueSet = new();
foreach (Entry? pair in buckets) foreach (Entry? pair in buckets)
{ {
if (pair != null) if (pair != null)
@@ -329,6 +329,8 @@ comments: true
UT_hash_handle hh; // 基于 uthash.h 实现 UT_hash_handle hh; // 基于 uthash.h 实现
}; };
typedef struct hashTable hashTable;
/* 哈希表查询 */ /* 哈希表查询 */
hashTable *find(hashTable *h, int key) { hashTable *find(hashTable *h, int key) {
hashTable *tmp; hashTable *tmp;
+6
View File
@@ -1043,6 +1043,8 @@ comments: true
struct doublyListNode *prev; // 前驱节点 struct doublyListNode *prev; // 前驱节点
}; };
typedef struct doublyListNode doublyListNode;
/* 构造函数 */ /* 构造函数 */
doublyListNode *newDoublyListNode(int num) { doublyListNode *newDoublyListNode(int num) {
doublyListNode *new = (doublyListNode *)malloc(sizeof(doublyListNode)); doublyListNode *new = (doublyListNode *)malloc(sizeof(doublyListNode));
@@ -1063,6 +1065,8 @@ comments: true
int queSize; // 双向队列的长度 int queSize; // 双向队列的长度
}; };
typedef struct linkedListDeque linkedListDeque;
/* 构造j */ /* 构造j */
linkedListDeque *newLinkedListDeque() { linkedListDeque *newLinkedListDeque() {
linkedListDeque *deque = (linkedListDeque *)malloc(sizeof(linkedListDeque)); linkedListDeque *deque = (linkedListDeque *)malloc(sizeof(linkedListDeque));
@@ -2166,6 +2170,8 @@ comments: true
int queCapacity; // 队列容量 int queCapacity; // 队列容量
}; };
typedef struct arrayDeque arrayDeque;
/* 构造函数 */ /* 构造函数 */
arrayDeque *newArrayDeque(int capacity) { arrayDeque *newArrayDeque(int capacity) {
arrayDeque *deque = (arrayDeque *)malloc(sizeof(arrayDeque)); arrayDeque *deque = (arrayDeque *)malloc(sizeof(arrayDeque));
+4
View File
@@ -685,6 +685,8 @@ comments: true
int queSize; int queSize;
}; };
typedef struct linkedListQueue linkedListQueue;
/* 构造函数 */ /* 构造函数 */
linkedListQueue *newLinkedListQueue() { linkedListQueue *newLinkedListQueue() {
linkedListQueue *queue = (linkedListQueue *)malloc(sizeof(linkedListQueue)); linkedListQueue *queue = (linkedListQueue *)malloc(sizeof(linkedListQueue));
@@ -1459,6 +1461,8 @@ comments: true
int queCapacity; // 队列容量 int queCapacity; // 队列容量
}; };
typedef struct arrayQueue arrayQueue;
/* 构造函数 */ /* 构造函数 */
arrayQueue *newArrayQueue(int capacity) { arrayQueue *newArrayQueue(int capacity) {
arrayQueue *queue = (arrayQueue *)malloc(sizeof(arrayQueue)); arrayQueue *queue = (arrayQueue *)malloc(sizeof(arrayQueue));
+4
View File
@@ -636,6 +636,8 @@ comments: true
int size; // 栈的长度 int size; // 栈的长度
}; };
typedef struct linkedListStack linkedListStack;
/* 构造函数 */ /* 构造函数 */
linkedListStack *newLinkedListStack() { linkedListStack *newLinkedListStack() {
linkedListStack *s = malloc(sizeof(linkedListStack)); linkedListStack *s = malloc(sizeof(linkedListStack));
@@ -1204,6 +1206,8 @@ comments: true
int size; int size;
}; };
typedef struct arrayStack arrayStack;
/* 构造函数 */ /* 构造函数 */
arrayStack *newArrayStack() { arrayStack *newArrayStack() {
arrayStack *s = malloc(sizeof(arrayStack)); arrayStack *s = malloc(sizeof(arrayStack));