This commit is contained in:
krahets
2023-09-02 23:53:33 +08:00
parent 503ca797b8
commit ca7b5c0ac2
12 changed files with 117 additions and 50 deletions

View File

@@ -273,9 +273,32 @@ status: new
=== "Swift"
```swift title="binary_search_recur.swift"
[class]{}-[func]{dfs}
/* 二分查找:问题 f(i, j) */
func dfs(nums: [Int], target: Int, i: Int, j: Int) -> Int {
// 若区间为空,代表无目标元素,则返回 -1
if i > j {
return -1
}
// 计算中点索引 m
let m = (i + j) / 2
if nums[m] < target {
// 递归子问题 f(m+1, j)
return dfs(nums: nums, target: target, i: m + 1, j: j)
} else if nums[m] > target {
// 递归子问题 f(i, m-1)
return dfs(nums: nums, target: target, i: i, j: m - 1)
} else {
// 找到目标元素,返回其索引
return m
}
}
[class]{}-[func]{binarySearch}
/* 二分查找 */
func binarySearch(nums: [Int], target: Int) -> Int {
let n = nums.count
// 求解问题 f(0, n-1)
return dfs(nums: nums, target: target, i: 0, j: n - 1)
}
```
=== "Zig"

View File

@@ -316,9 +316,30 @@ status: new
=== "Swift"
```swift title="build_tree.swift"
[class]{}-[func]{dfs}
/* 构建二叉树:分治 */
func dfs(preorder: [Int], inorder: [Int], hmap: [Int: Int], i: Int, l: Int, r: Int) -> TreeNode? {
// 子树区间为空时终止
if r - l < 0 {
return nil
}
// 初始化根节点
let root = TreeNode(x: preorder[i])
// 查询 m ,从而划分左右子树
let m = hmap[preorder[i]]!
// 子问题:构建左子树
root.left = dfs(preorder: preorder, inorder: inorder, hmap: hmap, i: i + 1, l: l, r: m - 1)
// 子问题:构建右子树
root.right = dfs(preorder: preorder, inorder: inorder, hmap: hmap, i: i + 1 + m - l, l: m + 1, r: r)
// 返回根节点
return root
}
[class]{}-[func]{buildTree}
/* 构建二叉树 */
func buildTree(preorder: [Int], inorder: [Int]) -> TreeNode? {
// 初始化哈希表,存储 inorder 元素到索引的映射
let hmap = inorder.enumerated().reduce(into: [:]) { $0[$1.element] = $1.offset }
return dfs(preorder: preorder, inorder: inorder, hmap: hmap, i: 0, l: 0, r: inorder.count - 1)
}
```
=== "Zig"

View File

@@ -159,7 +159,7 @@ status: new
}
/* 求解汉诺塔 */
void hanota(vector<int> &A, vector<int> &B, vector<int> &C) {
void solveHanota(vector<int> &A, vector<int> &B, vector<int> &C) {
int n = A.size();
// 将 A 顶部 n 个圆盘借助 B 移到 C
dfs(n, A, B, C);
@@ -189,7 +189,7 @@ status: new
# 子问题 f(i-1) :将 buf 顶部 i-1 个圆盘借助 src 移到 tar
dfs(i - 1, buf, src, tar)
def hanota(A: list[int], B: list[int], C: list[int]):
def solve_hanota(A: list[int], B: list[int], C: list[int]):
"""求解汉诺塔"""
n = len(A)
# 将 A 顶部 n 个圆盘借助 B 移到 C
@@ -225,7 +225,7 @@ status: new
}
/* 求解汉诺塔 */
func hanota(A, B, C *list.List) {
func solveHanota(A, B, C *list.List) {
n := A.Len()
// 将 A 顶部 n 个圆盘借助 B 移到 C
dfsHanota(n, A, B, C)
@@ -259,7 +259,7 @@ status: new
}
/* 求解汉诺塔 */
function hanota(A, B, C) {
function solveHanota(A, B, C) {
const n = A.length;
// 将 A 顶部 n 个圆盘借助 B 移到 C
dfs(n, A, B, C);
@@ -293,7 +293,7 @@ status: new
}
/* 求解汉诺塔 */
function hanota(A: number[], B: number[], C: number[]): void {
function solveHanota(A: number[], B: number[], C: number[]): void {
const n = A.length;
// 将 A 顶部 n 个圆盘借助 B 移到 C
dfs(n, A, B, C);
@@ -307,7 +307,7 @@ status: new
[class]{}-[func]{dfs}
[class]{}-[func]{hanota}
[class]{}-[func]{solveHanota}
```
=== "C#"
@@ -348,11 +348,36 @@ status: new
=== "Swift"
```swift title="hanota.swift"
[class]{}-[func]{move}
/* 移动一个圆盘 */
func move(src: inout [Int], tar: inout [Int]) {
// 从 src 顶部拿出一个圆盘
let pan = src.popLast()!
// 将圆盘放入 tar 顶部
tar.append(pan)
}
[class]{}-[func]{dfs}
/* 求解汉诺塔:问题 f(i) */
func dfs(i: Int, src: inout [Int], buf: inout [Int], tar: inout [Int]) {
// 若 src 只剩下一个圆盘,则直接将其移到 tar
if i == 1 {
move(src: &src, tar: &tar)
return
}
// 子问题 f(i-1) :将 src 顶部 i-1 个圆盘借助 tar 移到 buf
dfs(i: i - 1, src: &src, buf: &tar, tar: &buf)
// 子问题 f(1) :将 src 剩余一个圆盘移到 tar
move(src: &src, tar: &tar)
// 子问题 f(i-1) :将 buf 顶部 i-1 个圆盘借助 src 移到 tar
dfs(i: i - 1, src: &buf, buf: &src, tar: &tar)
}
[class]{}-[func]{hanota}
/* 求解汉诺塔 */
func solveHanota(A: inout [Int], B: inout [Int], C: inout [Int]) {
let n = A.count
// 列表尾部是柱子顶部
// 将 src 顶部 n 个圆盘借助 B 移到 C
dfs(i: n, src: &A, buf: &B, tar: &C)
}
```
=== "Zig"
@@ -362,7 +387,7 @@ status: new
[class]{}-[func]{dfs}
[class]{}-[func]{hanota}
[class]{}-[func]{solveHanota}
```
=== "Dart"
@@ -392,7 +417,7 @@ status: new
}
/* 求解汉诺塔 */
void hanota(List<int> A, List<int> B, List<int> C) {
void solveHanota(List<int> A, List<int> B, List<int> C) {
int n = A.length;
// 将 A 顶部 n 个圆盘借助 B 移到 C
dfs(n, A, B, C);
@@ -426,7 +451,7 @@ status: new
}
/* 求解汉诺塔 */
fn hanota(A: &mut Vec<i32>, B: &mut Vec<i32>, C: &mut Vec<i32>) {
fn solve_hanota(A: &mut Vec<i32>, B: &mut Vec<i32>, C: &mut Vec<i32>) {
let n = A.len() as i32;
// 将 A 顶部 n 个圆盘借助 B 移到 C
dfs(n, A, B, C);