This commit is contained in:
krahets
2023-07-19 01:44:39 +08:00
parent ec6f3fd337
commit 1184c791c5
14 changed files with 563 additions and 42 deletions
@@ -160,9 +160,32 @@ status: new
=== "C#"
```csharp title="binary_search_recur.cs"
[class]{binary_search_recur}-[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]{binary_search_recur}-[func]{binarySearch}
/* 二分查找 */
int binarySearch(int[] nums, int target) {
int n = nums.Length;
// 求解问题 f(0, n-1)
return dfs(nums, target, 0, n - 1);
}
```
=== "Swift"
@@ -195,9 +195,33 @@ status: new
=== "C#"
```csharp title="build_tree.cs"
[class]{build_tree}-[func]{dfs}
/* 构建二叉树:分治 */
TreeNode dfs(int[] preorder, int[] inorder, Dictionary<int, int> hmap, int i, int l, int r) {
// 子树区间为空时终止
if (r - l < 0)
return null;
// 初始化根节点
TreeNode root = new TreeNode(preorder[i]);
// 查询 m ,从而划分左右子树
int m = hmap[preorder[i]];
// 子问题:构建左子树
root.left = dfs(preorder, inorder, hmap, i + 1, l, m - 1);
// 子问题:构建右子树
root.right = dfs(preorder, inorder, hmap, i + 1 + m - l, m + 1, r);
// 返回根节点
return root;
}
[class]{build_tree}-[func]{buildTree}
/* 构建二叉树 */
TreeNode buildTree(int[] preorder, int[] inorder) {
// 初始化哈希表,存储 inorder 元素到索引的映射
Dictionary<int, int> hmap = new Dictionary<int, int>();
for (int i = 0; i < inorder.Length; i++) {
hmap.TryAdd(inorder[i], i);
}
TreeNode root = dfs(preorder, inorder, hmap, 0, 0, inorder.Length - 1);
return root;
}
```
=== "Swift"
+29 -4
View File
@@ -112,7 +112,7 @@ status: new
}
/* 求解汉诺塔 */
void hanota(List<Integer> A, List<Integer> B, List<Integer> C) {
void solveHanota(List<Integer> A, List<Integer> B, List<Integer> C) {
int n = A.size();
// 将 A 顶部 n 个圆盘借助 B 移到 C
dfs(n, A, B, C);
@@ -227,11 +227,36 @@ status: new
=== "C#"
```csharp title="hanota.cs"
[class]{hanota}-[func]{move}
/* 移动一个圆盘 */
void move(List<int> src, List<int> tar) {
// 从 src 顶部拿出一个圆盘
int pan = src[^1];
src.RemoveAt(src.Count - 1);
// 将圆盘放入 tar 顶部
tar.Add(pan);
}
[class]{hanota}-[func]{dfs}
/* 求解汉诺塔:问题 f(i) */
void dfs(int i, List<int> src, List<int> buf, List<int> 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]{hanota}-[func]{hanota}
/* 求解汉诺塔 */
void solveHanota(List<int> A, List<int> B, List<int> C) {
int n = A.Count;
// 将 A 顶部 n 个圆盘借助 B 移到 C
dfs(n, A, B, C);
}
```
=== "Swift"
+1 -1
View File
@@ -11,6 +11,6 @@ status: new
- 引入分治策略往往可以带来算法效率的提升。一方面,分治策略减少了计算吧操作数量;另一方面,分治后有利于系统的并行优化。
- 分治既可以解决许多算法问题,也广泛应用于数据结构与算法设计中,处处可见其身影。
- 相较于暴力搜索,自适应搜索效率更高。时间复杂度为 $O(\log n)$ 的搜索算法通常都是基于分治策略实现的。
- 二分查找也体现了分治思想,我们可以通过递归分治实现二分查找。
- 二分查找是分治思想的另一个典型应用,它不包含将子问题的解进行合并的步骤。我们可以通过递归分治实现二分查找。
- 在构建二叉树问题中,构建树(原问题)可以被划分为构建左子树和右子树(子问题),其可以通过划分前序遍历和中序遍历的索引区间来实现。
- 在汉诺塔问题中,一个规模为 $n$ 的问题可以被划分为两个规模为 $n-1$ 的子问题和一个规模为 $1$ 的子问题。按顺序解决这三个子问题后,原问题随之得到解决。