mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-12 03:40:58 +00:00
build
This commit is contained in:
@@ -139,7 +139,7 @@ comments: true
|
||||
|
||||
```csharp title="binary_search_recur.cs"
|
||||
/* 二分查找:问题 f(i, j) */
|
||||
int dfs(int[] nums, int target, int i, int j) {
|
||||
int Dfs(int[] nums, int target, int i, int j) {
|
||||
// 若区间为空,代表无目标元素,则返回 -1
|
||||
if (i > j) {
|
||||
return -1;
|
||||
@@ -148,10 +148,10 @@ comments: true
|
||||
int m = (i + j) / 2;
|
||||
if (nums[m] < target) {
|
||||
// 递归子问题 f(m+1, j)
|
||||
return dfs(nums, target, m + 1, j);
|
||||
return Dfs(nums, target, m + 1, j);
|
||||
} else if (nums[m] > target) {
|
||||
// 递归子问题 f(i, m-1)
|
||||
return dfs(nums, target, i, m - 1);
|
||||
return Dfs(nums, target, i, m - 1);
|
||||
} else {
|
||||
// 找到目标元素,返回其索引
|
||||
return m;
|
||||
@@ -159,10 +159,10 @@ comments: true
|
||||
}
|
||||
|
||||
/* 二分查找 */
|
||||
int binarySearch(int[] nums, int target) {
|
||||
int BinarySearch(int[] nums, int target) {
|
||||
int n = nums.Length;
|
||||
// 求解问题 f(0, n-1)
|
||||
return dfs(nums, target, 0, n - 1);
|
||||
return Dfs(nums, target, 0, n - 1);
|
||||
}
|
||||
```
|
||||
|
||||
@@ -354,9 +354,32 @@ comments: true
|
||||
=== "C"
|
||||
|
||||
```c title="binary_search_recur.c"
|
||||
[class]{}-[func]{dfs}
|
||||
/* 二分查找:问题 f(i, j) */
|
||||
int dfs(int nums[], int target, int i, int j) {
|
||||
// 若区间为空,代表无目标元素,则返回 -1
|
||||
if (i > j) {
|
||||
return -1;
|
||||
}
|
||||
// 计算中点索引 m
|
||||
int m = (i + j) / 2;
|
||||
if (nums[m] < target) {
|
||||
// 递归子问题 f(m+1, j)
|
||||
return dfs(nums, target, m + 1, j);
|
||||
} else if (nums[m] > target) {
|
||||
// 递归子问题 f(i, m-1)
|
||||
return dfs(nums, target, i, m - 1);
|
||||
} else {
|
||||
// 找到目标元素,返回其索引
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
[class]{}-[func]{binarySearch}
|
||||
/* 二分查找 */
|
||||
int binarySearch(int nums[], int target, int numsSize) {
|
||||
int n = numsSize;
|
||||
// 求解问题 f(0, n-1)
|
||||
return dfs(nums, target, 0, n - 1);
|
||||
}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
@@ -172,30 +172,30 @@ comments: true
|
||||
|
||||
```csharp title="build_tree.cs"
|
||||
/* 构建二叉树:分治 */
|
||||
TreeNode dfs(int[] preorder, Dictionary<int, int> inorderMap, int i, int l, int r) {
|
||||
TreeNode Dfs(int[] preorder, Dictionary<int, int> inorderMap, int i, int l, int r) {
|
||||
// 子树区间为空时终止
|
||||
if (r - l < 0)
|
||||
return null;
|
||||
// 初始化根节点
|
||||
TreeNode root = new TreeNode(preorder[i]);
|
||||
TreeNode root = new(preorder[i]);
|
||||
// 查询 m ,从而划分左右子树
|
||||
int m = inorderMap[preorder[i]];
|
||||
// 子问题:构建左子树
|
||||
root.left = dfs(preorder, inorderMap, i + 1, l, m - 1);
|
||||
root.left = Dfs(preorder, inorderMap, i + 1, l, m - 1);
|
||||
// 子问题:构建右子树
|
||||
root.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);
|
||||
root.right = Dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);
|
||||
// 返回根节点
|
||||
return root;
|
||||
}
|
||||
|
||||
/* 构建二叉树 */
|
||||
TreeNode buildTree(int[] preorder, int[] inorder) {
|
||||
TreeNode BuildTree(int[] preorder, int[] inorder) {
|
||||
// 初始化哈希表,存储 inorder 元素到索引的映射
|
||||
Dictionary<int, int> inorderMap = new Dictionary<int, int>();
|
||||
Dictionary<int, int> inorderMap = new();
|
||||
for (int i = 0; i < inorder.Length; i++) {
|
||||
inorderMap.TryAdd(inorder[i], i);
|
||||
}
|
||||
TreeNode root = dfs(preorder, inorderMap, 0, 0, inorder.Length - 1);
|
||||
TreeNode root = Dfs(preorder, inorderMap, 0, 0, inorder.Length - 1);
|
||||
return root;
|
||||
}
|
||||
```
|
||||
|
||||
@@ -199,7 +199,7 @@ comments: true
|
||||
|
||||
```csharp title="hanota.cs"
|
||||
/* 移动一个圆盘 */
|
||||
void move(List<int> src, List<int> tar) {
|
||||
void Move(List<int> src, List<int> tar) {
|
||||
// 从 src 顶部拿出一个圆盘
|
||||
int pan = src[^1];
|
||||
src.RemoveAt(src.Count - 1);
|
||||
@@ -208,25 +208,25 @@ comments: true
|
||||
}
|
||||
|
||||
/* 求解汉诺塔:问题 f(i) */
|
||||
void dfs(int i, List<int> src, List<int> buf, List<int> tar) {
|
||||
void Dfs(int i, List<int> src, List<int> buf, List<int> tar) {
|
||||
// 若 src 只剩下一个圆盘,则直接将其移到 tar
|
||||
if (i == 1) {
|
||||
move(src, tar);
|
||||
Move(src, tar);
|
||||
return;
|
||||
}
|
||||
// 子问题 f(i-1) :将 src 顶部 i-1 个圆盘借助 tar 移到 buf
|
||||
dfs(i - 1, src, tar, buf);
|
||||
Dfs(i - 1, src, tar, buf);
|
||||
// 子问题 f(1) :将 src 剩余一个圆盘移到 tar
|
||||
move(src, tar);
|
||||
Move(src, tar);
|
||||
// 子问题 f(i-1) :将 buf 顶部 i-1 个圆盘借助 src 移到 tar
|
||||
dfs(i - 1, buf, src, tar);
|
||||
Dfs(i - 1, buf, src, tar);
|
||||
}
|
||||
|
||||
/* 求解汉诺塔 */
|
||||
void solveHanota(List<int> A, List<int> B, List<int> C) {
|
||||
void SolveHanota(List<int> A, List<int> B, List<int> C) {
|
||||
int n = A.Count;
|
||||
// 将 A 顶部 n 个圆盘借助 B 移到 C
|
||||
dfs(n, A, B, C);
|
||||
Dfs(n, A, B, C);
|
||||
}
|
||||
```
|
||||
|
||||
@@ -440,11 +440,38 @@ comments: true
|
||||
=== "C"
|
||||
|
||||
```c title="hanota.c"
|
||||
[class]{}-[func]{move}
|
||||
/* 移动一个圆盘 */
|
||||
void move(vector *src, vector *tar) {
|
||||
// 从 src 顶部拿出一个圆盘
|
||||
int *panTemp = vectorBack(src);
|
||||
int *pan = malloc(sizeof(int));
|
||||
*pan = *panTemp;
|
||||
vectorPopback(src);
|
||||
// 将圆盘放入 tar 顶部
|
||||
vectorPushback(tar, pan, sizeof(int));
|
||||
}
|
||||
|
||||
[class]{}-[func]{dfs}
|
||||
/* 求解汉诺塔:问题 f(i) */
|
||||
void dfs(int i, vector *src, vector *buf, vector *tar) {
|
||||
// 若 src 只剩下一个圆盘,则直接将其移到 tar
|
||||
if (i == 1) {
|
||||
move(src, tar);
|
||||
return;
|
||||
}
|
||||
// 子问题 f(i-1) :将 src 顶部 i-1 个圆盘借助 tar 移到 buf
|
||||
dfs(i - 1, src, tar, buf);
|
||||
// 子问题 f(1) :将 src 剩余一个圆盘移到 tar
|
||||
move(src, tar);
|
||||
// 子问题 f(i-1) :将 buf 顶部 i-1 个圆盘借助 src 移到 tar
|
||||
dfs(i - 1, buf, src, tar);
|
||||
}
|
||||
|
||||
[class]{}-[func]{solveHanota}
|
||||
/* 求解汉诺塔 */
|
||||
void solveHanota(vector *A, vector *B, vector *C) {
|
||||
int n = A->size;
|
||||
// 将 A 顶部 n 个圆盘借助 B 移到 C
|
||||
dfs(n, A, B, C);
|
||||
}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
Reference in New Issue
Block a user