mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-25 12:26:29 +08:00
refactor: use Package.swift to define executable task
This commit is contained in:
parent
6e8954672f
commit
377200a39a
7 changed files with 169 additions and 131 deletions
18
codes/swift/Package.swift
Normal file
18
codes/swift/Package.swift
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
// swift-tools-version: 5.7
|
||||||
|
|
||||||
|
import PackageDescription
|
||||||
|
|
||||||
|
let package = Package(
|
||||||
|
name: "HelloAlgo",
|
||||||
|
products: [
|
||||||
|
.executable(name: "time_complexity", targets: ["time_complexity"]),
|
||||||
|
.executable(name: "worst_best_time_complexity", targets: ["worst_best_time_complexity"]),
|
||||||
|
.executable(name: "space_complexity", targets: ["space_complexity"]),
|
||||||
|
],
|
||||||
|
targets: [
|
||||||
|
.target(name: "utils", path: "utils"),
|
||||||
|
.executableTarget(name: "time_complexity", path: "chapter_computational_complexity", sources: ["time_complexity.swift"]),
|
||||||
|
.executableTarget(name: "worst_best_time_complexity", path: "chapter_computational_complexity", sources: ["worst_best_time_complexity.swift"]),
|
||||||
|
.executableTarget(name: "space_complexity", dependencies: ["utils"], path: "chapter_computational_complexity", sources: ["space_complexity.swift"]),
|
||||||
|
]
|
||||||
|
)
|
|
@ -4,82 +4,7 @@
|
||||||
* Author: nuomi1 (nuomi1@qq.com)
|
* Author: nuomi1 (nuomi1@qq.com)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
class ListNode {
|
import utils
|
||||||
var val: Int
|
|
||||||
var next: ListNode?
|
|
||||||
|
|
||||||
init(x: Int) {
|
|
||||||
val = x
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class TreeNode {
|
|
||||||
var val: Int // 结点值
|
|
||||||
var height: Int // 结点高度
|
|
||||||
var left: TreeNode? // 左子结点引用
|
|
||||||
var right: TreeNode? // 右子结点引用
|
|
||||||
|
|
||||||
init(x: Int) {
|
|
||||||
val = x
|
|
||||||
height = 0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
enum PrintUtil {
|
|
||||||
private class Trunk {
|
|
||||||
var prev: Trunk?
|
|
||||||
var str: String
|
|
||||||
|
|
||||||
init(prev: Trunk?, str: String) {
|
|
||||||
self.prev = prev
|
|
||||||
self.str = str
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static func printTree(root: TreeNode?) {
|
|
||||||
printTree(root: root, prev: nil, isLeft: false)
|
|
||||||
}
|
|
||||||
|
|
||||||
private static func printTree(root: TreeNode?, prev: Trunk?, isLeft: Bool) {
|
|
||||||
if root == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
var prevStr = " "
|
|
||||||
let trunk = Trunk(prev: prev, str: prevStr)
|
|
||||||
|
|
||||||
printTree(root: root?.right, prev: trunk, isLeft: true)
|
|
||||||
|
|
||||||
if prev == nil {
|
|
||||||
trunk.str = "———"
|
|
||||||
} else if isLeft {
|
|
||||||
trunk.str = "/———"
|
|
||||||
prevStr = " |"
|
|
||||||
} else {
|
|
||||||
trunk.str = "\\———"
|
|
||||||
prev?.str = prevStr
|
|
||||||
}
|
|
||||||
|
|
||||||
showTrunks(p: trunk)
|
|
||||||
print(" \(root!.val)")
|
|
||||||
|
|
||||||
if prev != nil {
|
|
||||||
prev?.str = prevStr
|
|
||||||
}
|
|
||||||
trunk.str = " |"
|
|
||||||
|
|
||||||
printTree(root: root?.left, prev: trunk, isLeft: false)
|
|
||||||
}
|
|
||||||
|
|
||||||
private static func showTrunks(p: Trunk?) {
|
|
||||||
if p == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
showTrunks(p: p?.prev)
|
|
||||||
print(p!.str, terminator: "")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 函数
|
// 函数
|
||||||
@discardableResult
|
@discardableResult
|
||||||
|
@ -153,20 +78,21 @@ func buildTree(n: Int) -> TreeNode? {
|
||||||
return root
|
return root
|
||||||
}
|
}
|
||||||
|
|
||||||
// Driver Code
|
@main
|
||||||
func main() {
|
enum SpaceComplexity {
|
||||||
let n = 5
|
// Driver Code
|
||||||
// 常数阶
|
static func main() {
|
||||||
constant(n: n)
|
let n = 5
|
||||||
// 线性阶
|
// 常数阶
|
||||||
linear(n: n)
|
constant(n: n)
|
||||||
linearRecur(n: n)
|
// 线性阶
|
||||||
// 平方阶
|
linear(n: n)
|
||||||
quadratic(n: n)
|
linearRecur(n: n)
|
||||||
quadraticRecur(n: n)
|
// 平方阶
|
||||||
// 指数阶
|
quadratic(n: n)
|
||||||
let root = buildTree(n: n)
|
quadraticRecur(n: n)
|
||||||
PrintUtil.printTree(root: root)
|
// 指数阶
|
||||||
|
let root = buildTree(n: n)
|
||||||
|
PrintUtil.printTree(root: root)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
main()
|
|
||||||
|
|
|
@ -131,40 +131,41 @@ func factorialRecur(n: Int) -> Int {
|
||||||
return count
|
return count
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
@main
|
||||||
// 可以修改 n 运行,体会一下各种复杂度的操作数量变化趋势
|
enum TimeComplexity {
|
||||||
let n = 8
|
static func main() {
|
||||||
print("输入数据大小 n =", n)
|
// 可以修改 n 运行,体会一下各种复杂度的操作数量变化趋势
|
||||||
|
let n = 8
|
||||||
|
print("输入数据大小 n =", n)
|
||||||
|
|
||||||
var count = constant(n: n)
|
var count = constant(n: n)
|
||||||
print("常数阶的计算操作数量 =", count)
|
print("常数阶的计算操作数量 =", count)
|
||||||
|
|
||||||
count = linear(n: n)
|
count = linear(n: n)
|
||||||
print("线性阶的计算操作数量 =", count)
|
print("线性阶的计算操作数量 =", count)
|
||||||
count = arrayTraversal(nums: Array(repeating: 0, count: n))
|
count = arrayTraversal(nums: Array(repeating: 0, count: n))
|
||||||
print("线性阶(遍历数组)的计算操作数量 =", count)
|
print("线性阶(遍历数组)的计算操作数量 =", count)
|
||||||
|
|
||||||
count = quadratic(n: n)
|
count = quadratic(n: n)
|
||||||
print("平方阶的计算操作数量 =", count)
|
print("平方阶的计算操作数量 =", count)
|
||||||
var nums = Array(sequence(first: n, next: { $0 > 0 ? $0 - 1 : nil })) // [n,n-1,...,2,1]
|
var nums = Array(sequence(first: n, next: { $0 > 0 ? $0 - 1 : nil })) // [n,n-1,...,2,1]
|
||||||
count = bubbleSort(nums: &nums)
|
count = bubbleSort(nums: &nums)
|
||||||
print("平方阶(冒泡排序)的计算操作数量 =", count)
|
print("平方阶(冒泡排序)的计算操作数量 =", count)
|
||||||
|
|
||||||
count = exponential(n: n)
|
count = exponential(n: n)
|
||||||
print("指数阶(循环实现)的计算操作数量 =", count)
|
print("指数阶(循环实现)的计算操作数量 =", count)
|
||||||
count = expRecur(n: n)
|
count = expRecur(n: n)
|
||||||
print("指数阶(递归实现)的计算操作数量 =", count)
|
print("指数阶(递归实现)的计算操作数量 =", count)
|
||||||
|
|
||||||
count = logarithmic(n: n)
|
count = logarithmic(n: n)
|
||||||
print("对数阶(循环实现)的计算操作数量 =", count)
|
print("对数阶(循环实现)的计算操作数量 =", count)
|
||||||
count = logRecur(n: n)
|
count = logRecur(n: n)
|
||||||
print("对数阶(递归实现)的计算操作数量 =", count)
|
print("对数阶(递归实现)的计算操作数量 =", count)
|
||||||
|
|
||||||
count = linearLogRecur(n: Double(n))
|
count = linearLogRecur(n: Double(n))
|
||||||
print("线性对数阶(递归实现)的计算操作数量 =", count)
|
print("线性对数阶(递归实现)的计算操作数量 =", count)
|
||||||
|
|
||||||
count = factorialRecur(n: n)
|
count = factorialRecur(n: n)
|
||||||
print("阶乘阶(递归实现)的计算操作数量 =", count)
|
print("阶乘阶(递归实现)的计算操作数量 =", count)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
main()
|
|
||||||
|
|
|
@ -23,15 +23,16 @@ func findOne(nums: [Int]) -> Int {
|
||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
|
|
||||||
// Driver Code
|
@main
|
||||||
func main() {
|
enum WorstBestTimeComplexity {
|
||||||
for _ in 0 ..< 10 {
|
// Driver Code
|
||||||
let n = 100
|
static func main() {
|
||||||
let nums = randomNumbers(n: n)
|
for _ in 0 ..< 10 {
|
||||||
let index = findOne(nums: nums)
|
let n = 100
|
||||||
print("数组 [ 1, 2, ..., n ] 被打乱后 =", nums)
|
let nums = randomNumbers(n: n)
|
||||||
print("数字 1 的索引为", index)
|
let index = findOne(nums: nums)
|
||||||
|
print("数组 [ 1, 2, ..., n ] 被打乱后 =", nums)
|
||||||
|
print("数字 1 的索引为", index)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
main()
|
|
||||||
|
|
14
codes/swift/utils/ListNode.swift
Normal file
14
codes/swift/utils/ListNode.swift
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
/*
|
||||||
|
* File: ListNode.swift
|
||||||
|
* Created Time: 2023-01-02
|
||||||
|
* Author: nuomi1 (nuomi1@qq.com)
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class ListNode {
|
||||||
|
public var val: Int // 结点值
|
||||||
|
public var next: ListNode? // 后继结点引用
|
||||||
|
|
||||||
|
public init(x: Int) {
|
||||||
|
val = x
|
||||||
|
}
|
||||||
|
}
|
61
codes/swift/utils/PrintUtil.swift
Normal file
61
codes/swift/utils/PrintUtil.swift
Normal file
|
@ -0,0 +1,61 @@
|
||||||
|
/*
|
||||||
|
* File: PrintUtil.swift
|
||||||
|
* Created Time: 2023-01-02
|
||||||
|
* Author: nuomi1 (nuomi1@qq.com)
|
||||||
|
*/
|
||||||
|
|
||||||
|
public enum PrintUtil {
|
||||||
|
private class Trunk {
|
||||||
|
var prev: Trunk?
|
||||||
|
var str: String
|
||||||
|
|
||||||
|
init(prev: Trunk?, str: String) {
|
||||||
|
self.prev = prev
|
||||||
|
self.str = str
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static func printTree(root: TreeNode?) {
|
||||||
|
printTree(root: root, prev: nil, isLeft: false)
|
||||||
|
}
|
||||||
|
|
||||||
|
private static func printTree(root: TreeNode?, prev: Trunk?, isLeft: Bool) {
|
||||||
|
if root == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var prevStr = " "
|
||||||
|
let trunk = Trunk(prev: prev, str: prevStr)
|
||||||
|
|
||||||
|
printTree(root: root?.right, prev: trunk, isLeft: true)
|
||||||
|
|
||||||
|
if prev == nil {
|
||||||
|
trunk.str = "———"
|
||||||
|
} else if isLeft {
|
||||||
|
trunk.str = "/———"
|
||||||
|
prevStr = " |"
|
||||||
|
} else {
|
||||||
|
trunk.str = "\\———"
|
||||||
|
prev?.str = prevStr
|
||||||
|
}
|
||||||
|
|
||||||
|
showTrunks(p: trunk)
|
||||||
|
print(" \(root!.val)")
|
||||||
|
|
||||||
|
if prev != nil {
|
||||||
|
prev?.str = prevStr
|
||||||
|
}
|
||||||
|
trunk.str = " |"
|
||||||
|
|
||||||
|
printTree(root: root?.left, prev: trunk, isLeft: false)
|
||||||
|
}
|
||||||
|
|
||||||
|
private static func showTrunks(p: Trunk?) {
|
||||||
|
if p == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
showTrunks(p: p?.prev)
|
||||||
|
print(p!.str, terminator: "")
|
||||||
|
}
|
||||||
|
}
|
17
codes/swift/utils/TreeNode.swift
Normal file
17
codes/swift/utils/TreeNode.swift
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
/*
|
||||||
|
* File: TreeNode.swift
|
||||||
|
* Created Time: 2023-01-02
|
||||||
|
* Author: nuomi1 (nuomi1@qq.com)
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class TreeNode {
|
||||||
|
public var val: Int // 结点值
|
||||||
|
public var height: Int // 结点高度
|
||||||
|
public var left: TreeNode? // 左子结点引用
|
||||||
|
public var right: TreeNode? // 右子结点引用
|
||||||
|
|
||||||
|
public init(x: Int) {
|
||||||
|
val = x
|
||||||
|
height = 0
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue