From 17ff091a03b133cb21bc1594fd0c36610da12d64 Mon Sep 17 00:00:00 2001 From: nuomi1 Date: Fri, 3 Mar 2023 21:22:23 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20review=20Swift=20codes=20for=20chap?= =?UTF-8?q?ter=5Fcomputational=5Fcomplexity=20art=E2=80=A6=20(#396)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * refactor: review Swift codes for chapter_computational_complexity articles * Update time_complexity.swift * Update time_complexity.swift --------- Co-authored-by: Yudong Jin --- .../leetcode_two_sum.swift | 1 + .../time_complexity.swift | 10 +++++----- .../time_complexity.md | 8 ++++---- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/codes/swift/chapter_computational_complexity/leetcode_two_sum.swift b/codes/swift/chapter_computational_complexity/leetcode_two_sum.swift index 3ecf97b05..46fb35912 100644 --- a/codes/swift/chapter_computational_complexity/leetcode_two_sum.swift +++ b/codes/swift/chapter_computational_complexity/leetcode_two_sum.swift @@ -33,6 +33,7 @@ func twoSumHashTable(nums: [Int], target: Int) -> [Int] { @main enum LeetcodeTwoSum { + /* Driver Code */ static func main() { // ======= Test Case ======= let nums = [2, 7, 11, 15] diff --git a/codes/swift/chapter_computational_complexity/time_complexity.swift b/codes/swift/chapter_computational_complexity/time_complexity.swift index 1b1027ef2..121ae240e 100644 --- a/codes/swift/chapter_computational_complexity/time_complexity.swift +++ b/codes/swift/chapter_computational_complexity/time_complexity.swift @@ -88,7 +88,7 @@ func expRecur(n: Int) -> Int { } /* 对数阶(循环实现) */ -func logarithmic(n: Int) -> Int { +func logarithmic(n: Double) -> Int { var count = 0 var n = n while n > 1 { @@ -99,7 +99,7 @@ func logarithmic(n: Int) -> Int { } /* 对数阶(递归实现) */ -func logRecur(n: Int) -> Int { +func logRecur(n: Double) -> Int { if n <= 1 { return 0 } @@ -112,7 +112,7 @@ func linearLogRecur(n: Double) -> Int { return 1 } var count = linearLogRecur(n: n / 2) + linearLogRecur(n: n / 2) - for _ in 0 ..< Int(n) { + for _ in sequence(first: 0, next: { $0 < n - 1 ? $0 + 1 : nil }) { count += 1 } return count @@ -158,9 +158,9 @@ enum TimeComplexity { count = expRecur(n: n) print("指数阶(递归实现)的计算操作数量 = \(count)") - count = logarithmic(n: n) + count = logarithmic(n: Double(n)) print("对数阶(循环实现)的计算操作数量 = \(count)") - count = logRecur(n: n) + count = logRecur(n: Double(n)) print("对数阶(递归实现)的计算操作数量 = \(count)") count = linearLogRecur(n: Double(n)) diff --git a/docs/chapter_computational_complexity/time_complexity.md b/docs/chapter_computational_complexity/time_complexity.md index a768df4e2..1d937f2dc 100755 --- a/docs/chapter_computational_complexity/time_complexity.md +++ b/docs/chapter_computational_complexity/time_complexity.md @@ -138,7 +138,7 @@ $$ ```swift title="" // 在某运行平台下 - func algorithm(_ n: Int) { + func algorithm(n: Int) { var a = 2 // 1 ns a = a + 1 // 1 ns a = a * 2 // 10 ns @@ -340,19 +340,19 @@ $$ ```swift title="" // 算法 A 时间复杂度:常数阶 - func algorithmA(_ n: Int) { + func algorithmA(n: Int) { print(0) } // 算法 B 时间复杂度:线性阶 - func algorithmB(_ n: Int) { + func algorithmB(n: Int) { for _ in 0 ..< n { print(0) } } // 算法 C 时间复杂度:常数阶 - func algorithmC(_ n: Int) { + func algorithmC(n: Int) { for _ in 0 ..< 1000000 { print(0) }