Skip to content

Commit

Permalink
Merge branch 'krahets:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
coderonion authored Jan 3, 2023
2 parents 1b356ba + 4ac254d commit 07827ae
Show file tree
Hide file tree
Showing 9 changed files with 538 additions and 46 deletions.
130 changes: 130 additions & 0 deletions codes/swift/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
# Created by https://www.toptal.com/developers/gitignore/api/objective-c,swift,swiftpackagemanager
# Edit at https://www.toptal.com/developers/gitignore?templates=objective-c,swift,swiftpackagemanager

### Objective-C ###
# Xcode
#
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore

## User settings
xcuserdata/

## compatibility with Xcode 8 and earlier (ignoring not required starting Xcode 9)
*.xcscmblueprint
*.xccheckout

## compatibility with Xcode 3 and earlier (ignoring not required starting Xcode 4)
build/
DerivedData/
*.moved-aside
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3

## Obj-C/Swift specific
*.hmap

## App packaging
*.ipa
*.dSYM.zip
*.dSYM

# CocoaPods
# We recommend against adding the Pods directory to your .gitignore. However
# you should judge for yourself, the pros and cons are mentioned at:
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
# Pods/
# Add this line if you want to avoid checking in source code from the Xcode workspace
# *.xcworkspace

# Carthage
# Add this line if you want to avoid checking in source code from Carthage dependencies.
# Carthage/Checkouts

Carthage/Build/

# fastlane
# It is recommended to not store the screenshots in the git repo.
# Instead, use fastlane to re-generate the screenshots whenever they are needed.
# For more information about the recommended setup visit:
# https://docs.fastlane.tools/best-practices/source-control/#source-control

fastlane/report.xml
fastlane/Preview.html
fastlane/screenshots/**/*.png
fastlane/test_output

# Code Injection
# After new code Injection tools there's a generated folder /iOSInjectionProject
# https://github.com/johnno1962/injectionforxcode

iOSInjectionProject/

### Objective-C Patch ###

### Swift ###
# Xcode
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore






## Playgrounds
timeline.xctimeline
playground.xcworkspace

# Swift Package Manager
# Add this line if you want to avoid checking in source code from Swift Package Manager dependencies.
# Packages/
# Package.pins
# Package.resolved
# *.xcodeproj
# Xcode automatically generates this directory with a .xcworkspacedata file and xcuserdata
# hence it is not needed unless you have added a package configuration file to your project
# .swiftpm

.build/

# CocoaPods
# We recommend against adding the Pods directory to your .gitignore. However
# you should judge for yourself, the pros and cons are mentioned at:
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
# Pods/
# Add this line if you want to avoid checking in source code from the Xcode workspace
# *.xcworkspace

# Carthage
# Add this line if you want to avoid checking in source code from Carthage dependencies.
# Carthage/Checkouts


# Accio dependency management
Dependencies/
.accio/

# fastlane
# It is recommended to not store the screenshots in the git repo.
# Instead, use fastlane to re-generate the screenshots whenever they are needed.
# For more information about the recommended setup visit:
# https://docs.fastlane.tools/best-practices/source-control/#source-control


# Code Injection
# After new code Injection tools there's a generated folder /iOSInjectionProject
# https://github.com/johnno1962/injectionforxcode


### SwiftPackageManager ###
Packages
xcuserdata
*.xcodeproj


# End of https://www.toptal.com/developers/gitignore/api/objective-c,swift,swiftpackagemanager
18 changes: 18 additions & 0 deletions codes/swift/Package.swift
Original file line number Diff line number Diff line change
@@ -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"]),
]
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* File: space_complexity.swift
* Created Time: 2023-01-01
* Author: nuomi1 ([email protected])
*/

import utils

// 函数
@discardableResult
func function() -> Int {
// do something
return 0
}

// 常数阶
func constant(n: Int) {
// 常量、变量、对象占用 O(1) 空间
let a = 0
var b = 0
let nums = Array(repeating: 0, count: 10000)
let node = ListNode(x: 0)
// 循环中的变量占用 O(1) 空间
for _ in 0 ..< n {
let c = 0
}
// 循环中的函数占用 O(1) 空间
for _ in 0 ..< n {
function()
}
}

// 线性阶
func linear(n: Int) {
// 长度为 n 的数组占用 O(n) 空间
let nums = Array(repeating: 0, count: n)
// 长度为 n 的列表占用 O(n) 空间
let nodes = (0 ..< n).map { ListNode(x: $0) }
// 长度为 n 的哈希表占用 O(n) 空间
let map = Dictionary(uniqueKeysWithValues: (0 ..< n).map { ($0, "\($0)") })
}

// 线性阶(递归实现)
func linearRecur(n: Int) {
print("递归 n = \(n)")
if n == 1 {
return
}
linearRecur(n: n - 1)
}

// 平方阶
func quadratic(n: Int) {
// 二维列表占用 O(n^2) 空间
let numList = Array(repeating: Array(repeating: 0, count: n), count: n)
}

// 平方阶(递归实现)
@discardableResult
func quadraticRecur(n: Int) -> Int {
if n <= 0 {
return 0
}
// 数组 nums 长度为 n, n-1, ..., 2, 1
let nums = Array(repeating: 0, count: n)
print("递归 n = \(n) 中的 nums 长度 = \(nums.count)")
return quadraticRecur(n: n - 1)
}

// 指数阶(建立满二叉树)
func buildTree(n: Int) -> TreeNode? {
if n == 0 {
return nil
}
let root = TreeNode(x: 0)
root.left = buildTree(n: n - 1)
root.right = buildTree(n: n - 1)
return root
}

@main
enum SpaceComplexity {
// Driver Code
static func main() {
let n = 5
// 常数阶
constant(n: n)
// 线性阶
linear(n: n)
linearRecur(n: n)
// 平方阶
quadratic(n: n)
quadraticRecur(n: n)
// 指数阶
let root = buildTree(n: n)
PrintUtil.printTree(root: root)
}
}
73 changes: 37 additions & 36 deletions codes/swift/chapter_computational_complexity/time_complexity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,40 +131,41 @@ func factorialRecur(n: Int) -> Int {
return count
}

func main() {
// 可以修改 n 运行,体会一下各种复杂度的操作数量变化趋势
let n = 8
print("输入数据大小 n =", n)

var count = constant(n: n)
print("常数阶的计算操作数量 =", count)

count = linear(n: n)
print("线性阶的计算操作数量 =", count)
count = arrayTraversal(nums: Array(repeating: 0, count: n))
print("线性阶(遍历数组)的计算操作数量 =", count)

count = quadratic(n: n)
print("平方阶的计算操作数量 =", count)
var nums = Array(sequence(first: n, next: { $0 > 0 ? $0 - 1 : nil })) // [n,n-1,...,2,1]
count = bubbleSort(nums: &nums)
print("平方阶(冒泡排序)的计算操作数量 =", count)

count = exponential(n: n)
print("指数阶(循环实现)的计算操作数量 =", count)
count = expRecur(n: n)
print("指数阶(递归实现)的计算操作数量 =", count)

count = logarithmic(n: n)
print("对数阶(循环实现)的计算操作数量 =", count)
count = logRecur(n: n)
print("对数阶(递归实现)的计算操作数量 =", count)

count = linearLogRecur(n: Double(n))
print("线性对数阶(递归实现)的计算操作数量 =", count)

count = factorialRecur(n: n)
print("阶乘阶(递归实现)的计算操作数量 =", count)
@main
enum TimeComplexity {
static func main() {
// 可以修改 n 运行,体会一下各种复杂度的操作数量变化趋势
let n = 8
print("输入数据大小 n =", n)

var count = constant(n: n)
print("常数阶的计算操作数量 =", count)

count = linear(n: n)
print("线性阶的计算操作数量 =", count)
count = arrayTraversal(nums: Array(repeating: 0, count: n))
print("线性阶(遍历数组)的计算操作数量 =", count)

count = quadratic(n: n)
print("平方阶的计算操作数量 =", count)
var nums = Array(sequence(first: n, next: { $0 > 0 ? $0 - 1 : nil })) // [n,n-1,...,2,1]
count = bubbleSort(nums: &nums)
print("平方阶(冒泡排序)的计算操作数量 =", count)

count = exponential(n: n)
print("指数阶(循环实现)的计算操作数量 =", count)
count = expRecur(n: n)
print("指数阶(递归实现)的计算操作数量 =", count)

count = logarithmic(n: n)
print("对数阶(循环实现)的计算操作数量 =", count)
count = logRecur(n: n)
print("对数阶(递归实现)的计算操作数量 =", count)

count = linearLogRecur(n: Double(n))
print("线性对数阶(递归实现)的计算操作数量 =", count)

count = factorialRecur(n: n)
print("阶乘阶(递归实现)的计算操作数量 =", count)
}
}

main()
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@ func findOne(nums: [Int]) -> Int {
return -1
}

// Driver Code
func main() {
for _ in 0 ..< 10 {
let n = 100
let nums = randomNumbers(n: n)
let index = findOne(nums: nums)
print("数组 [ 1, 2, ..., n ] 被打乱后 =", nums)
print("数字 1 的索引为", index)
@main
enum WorstBestTimeComplexity {
// Driver Code
static func main() {
for _ in 0 ..< 10 {
let n = 100
let nums = randomNumbers(n: n)
let index = findOne(nums: nums)
print("数组 [ 1, 2, ..., n ] 被打乱后 =", nums)
print("数字 1 的索引为", index)
}
}
}

main()
14 changes: 14 additions & 0 deletions codes/swift/utils/ListNode.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* File: ListNode.swift
* Created Time: 2023-01-02
* Author: nuomi1 ([email protected])
*/

public class ListNode {
public var val: Int // 结点值
public var next: ListNode? // 后继结点引用

public init(x: Int) {
val = x
}
}
Loading

0 comments on commit 07827ae

Please sign in to comment.