Feature/chapter divide and conquer swift (#719)

* feat: add Swift codes for binary_search_recur article

* feat: add Swift codes for build_binary_tree_problem article

* feat: add Swift codes for hanota_problem article
pull/721/head
nuomi1 1 year ago committed by GitHub
parent 978857570f
commit b6ac6aa7d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -65,6 +65,10 @@ let package = Package(
.executable(name: "bucket_sort", targets: ["bucket_sort"]), .executable(name: "bucket_sort", targets: ["bucket_sort"]),
.executable(name: "counting_sort", targets: ["counting_sort"]), .executable(name: "counting_sort", targets: ["counting_sort"]),
.executable(name: "radix_sort", targets: ["radix_sort"]), .executable(name: "radix_sort", targets: ["radix_sort"]),
// chapter_divide_and_conquer
.executable(name: "binary_search_recur", targets: ["binary_search_recur"]),
.executable(name: "build_tree", targets: ["build_tree"]),
.executable(name: "hanota", targets: ["hanota"]),
// chapter_backtracking // chapter_backtracking
.executable(name: "preorder_traversal_i_compact", targets: ["preorder_traversal_i_compact"]), .executable(name: "preorder_traversal_i_compact", targets: ["preorder_traversal_i_compact"]),
.executable(name: "preorder_traversal_ii_compact", targets: ["preorder_traversal_ii_compact"]), .executable(name: "preorder_traversal_ii_compact", targets: ["preorder_traversal_ii_compact"]),
@ -155,6 +159,10 @@ let package = Package(
.executableTarget(name: "bucket_sort", path: "chapter_sorting", sources: ["bucket_sort.swift"]), .executableTarget(name: "bucket_sort", path: "chapter_sorting", sources: ["bucket_sort.swift"]),
.executableTarget(name: "counting_sort", path: "chapter_sorting", sources: ["counting_sort.swift"]), .executableTarget(name: "counting_sort", path: "chapter_sorting", sources: ["counting_sort.swift"]),
.executableTarget(name: "radix_sort", path: "chapter_sorting", sources: ["radix_sort.swift"]), .executableTarget(name: "radix_sort", path: "chapter_sorting", sources: ["radix_sort.swift"]),
// chapter_divide_and_conquer
.executableTarget(name: "binary_search_recur", path: "chapter_divide_and_conquer", sources: ["binary_search_recur.swift"]),
.executableTarget(name: "build_tree", dependencies: ["utils"], path: "chapter_divide_and_conquer", sources: ["build_tree.swift"]),
.executableTarget(name: "hanota", path: "chapter_divide_and_conquer", sources: ["hanota.swift"]),
// chapter_backtracking // chapter_backtracking
.executableTarget(name: "preorder_traversal_i_compact", dependencies: ["utils"], path: "chapter_backtracking", sources: ["preorder_traversal_i_compact.swift"]), .executableTarget(name: "preorder_traversal_i_compact", dependencies: ["utils"], path: "chapter_backtracking", sources: ["preorder_traversal_i_compact.swift"]),
.executableTarget(name: "preorder_traversal_ii_compact", dependencies: ["utils"], path: "chapter_backtracking", sources: ["preorder_traversal_ii_compact.swift"]), .executableTarget(name: "preorder_traversal_ii_compact", dependencies: ["utils"], path: "chapter_backtracking", sources: ["preorder_traversal_ii_compact.swift"]),

@ -0,0 +1,45 @@
/**
* File: binary_search_recur.swift
* Created Time: 2023-09-02
* Author: nuomi1 (nuomi1@qq.com)
*/
/* 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
}
}
/* */
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)
}
@main
enum BinarySearchRecur {
/* Driver Code */
static func main() {
let target = 6
let nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35]
//
let index = binarySearch(nums: nums, target: target)
print("目标元素 6 的索引 = \(index)")
}
}

@ -0,0 +1,47 @@
/**
* File: build_tree.swift
* Created Time: 2023-09-02
* Author: nuomi1 (nuomi1@qq.com)
*/
import utils
/* */
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
}
/* */
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)
}
@main
enum BuildTree {
/* Driver Code */
static func main() {
let preorder = [3, 9, 2, 1, 7]
let inorder = [9, 3, 1, 2, 7]
print("前序遍历 = \(preorder)")
print("中序遍历 = \(inorder)")
let root = buildTree(preorder: preorder, inorder: inorder)
print("构建的二叉树为:")
PrintUtil.printTree(root: root)
}
}

@ -0,0 +1,58 @@
/**
* File: hanota.swift
* Created Time: 2023-09-02
* Author: nuomi1 (nuomi1@qq.com)
*/
/* */
func move(src: inout [Int], tar: inout [Int]) {
// src
let pan = src.popLast()!
// tar
tar.append(pan)
}
/* 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)
}
/* */
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)
}
@main
enum Hanota {
/* Driver Code */
static func main() {
//
var A = [5, 4, 3, 2, 1]
var B: [Int] = []
var C: [Int] = []
print("初始状态下:")
print("A = \(A)")
print("B = \(B)")
print("C = \(C)")
solveHanota(A: &A, B: &B, C: &C)
print("圆盘移动完成后:")
print("A = \(A)")
print("B = \(B)")
print("C = \(C)")
}
}
Loading…
Cancel
Save