forked from krahets/hello-algo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'krahets:master' into master
- Loading branch information
Showing
9 changed files
with
538 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"]), | ||
] | ||
) |
98 changes: 98 additions & 0 deletions
98
codes/swift/chapter_computational_complexity/space_complexity.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
Oops, something went wrong.