mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 21:24:53 +08:00
style: update comment format
This commit is contained in:
@ -6,14 +6,14 @@
|
||||
|
||||
import utils
|
||||
|
||||
// 函数
|
||||
/* 函数 */
|
||||
@discardableResult
|
||||
func function() -> Int {
|
||||
// do something
|
||||
return 0
|
||||
}
|
||||
|
||||
// 常数阶
|
||||
/* 常数阶 */
|
||||
func constant(n: Int) {
|
||||
// 常量、变量、对象占用 O(1) 空间
|
||||
let a = 0
|
||||
@ -30,7 +30,7 @@ func constant(n: Int) {
|
||||
}
|
||||
}
|
||||
|
||||
// 线性阶
|
||||
/* 线性阶 */
|
||||
func linear(n: Int) {
|
||||
// 长度为 n 的数组占用 O(n) 空间
|
||||
let nums = Array(repeating: 0, count: n)
|
||||
@ -40,7 +40,7 @@ func linear(n: Int) {
|
||||
let map = Dictionary(uniqueKeysWithValues: (0 ..< n).map { ($0, "\($0)") })
|
||||
}
|
||||
|
||||
// 线性阶(递归实现)
|
||||
/* 线性阶(递归实现) */
|
||||
func linearRecur(n: Int) {
|
||||
print("递归 n = \(n)")
|
||||
if n == 1 {
|
||||
@ -49,13 +49,13 @@ func linearRecur(n: Int) {
|
||||
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 {
|
||||
@ -67,7 +67,7 @@ func quadraticRecur(n: Int) -> Int {
|
||||
return quadraticRecur(n: n - 1)
|
||||
}
|
||||
|
||||
// 指数阶(建立满二叉树)
|
||||
/* 指数阶(建立满二叉树) */
|
||||
func buildTree(n: Int) -> TreeNode? {
|
||||
if n == 0 {
|
||||
return nil
|
||||
@ -80,7 +80,7 @@ func buildTree(n: Int) -> TreeNode? {
|
||||
|
||||
@main
|
||||
enum SpaceComplexity {
|
||||
// Driver Code
|
||||
/* Driver Code */
|
||||
static func main() {
|
||||
let n = 5
|
||||
// 常数阶
|
||||
|
||||
@ -4,7 +4,7 @@
|
||||
* Author: nuomi1 (nuomi1@qq.com)
|
||||
*/
|
||||
|
||||
// 常数阶
|
||||
/* 常数阶 */
|
||||
func constant(n: Int) -> Int {
|
||||
var count = 0
|
||||
let size = 100_000
|
||||
@ -14,7 +14,7 @@ func constant(n: Int) -> Int {
|
||||
return count
|
||||
}
|
||||
|
||||
// 线性阶
|
||||
/* 线性阶 */
|
||||
func linear(n: Int) -> Int {
|
||||
var count = 0
|
||||
for _ in 0 ..< n {
|
||||
@ -23,7 +23,7 @@ func linear(n: Int) -> Int {
|
||||
return count
|
||||
}
|
||||
|
||||
// 线性阶(遍历数组)
|
||||
/* 线性阶(遍历数组) */
|
||||
func arrayTraversal(nums: [Int]) -> Int {
|
||||
var count = 0
|
||||
// 循环次数与数组长度成正比
|
||||
@ -33,7 +33,7 @@ func arrayTraversal(nums: [Int]) -> Int {
|
||||
return count
|
||||
}
|
||||
|
||||
// 平方阶
|
||||
/* 平方阶 */
|
||||
func quadratic(n: Int) -> Int {
|
||||
var count = 0
|
||||
// 循环次数与数组长度成平方关系
|
||||
@ -45,7 +45,7 @@ func quadratic(n: Int) -> Int {
|
||||
return count
|
||||
}
|
||||
|
||||
// 平方阶(冒泡排序)
|
||||
/* 平方阶(冒泡排序) */
|
||||
func bubbleSort(nums: inout [Int]) -> Int {
|
||||
var count = 0 // 计数器
|
||||
// 外循环:待排序元素数量为 n-1, n-2, ..., 1
|
||||
@ -64,7 +64,7 @@ func bubbleSort(nums: inout [Int]) -> Int {
|
||||
return count
|
||||
}
|
||||
|
||||
// 指数阶(循环实现)
|
||||
/* 指数阶(循环实现) */
|
||||
func exponential(n: Int) -> Int {
|
||||
var count = 0
|
||||
var base = 1
|
||||
@ -79,7 +79,7 @@ func exponential(n: Int) -> Int {
|
||||
return count
|
||||
}
|
||||
|
||||
// 指数阶(递归实现)
|
||||
/* 指数阶(递归实现) */
|
||||
func expRecur(n: Int) -> Int {
|
||||
if n == 1 {
|
||||
return 1
|
||||
@ -87,7 +87,7 @@ func expRecur(n: Int) -> Int {
|
||||
return expRecur(n: n - 1) + expRecur(n: n - 1) + 1
|
||||
}
|
||||
|
||||
// 对数阶(循环实现)
|
||||
/* 对数阶(循环实现) */
|
||||
func logarithmic(n: Int) -> Int {
|
||||
var count = 0
|
||||
var n = n
|
||||
@ -98,7 +98,7 @@ func logarithmic(n: Int) -> Int {
|
||||
return count
|
||||
}
|
||||
|
||||
// 对数阶(递归实现)
|
||||
/* 对数阶(递归实现) */
|
||||
func logRecur(n: Int) -> Int {
|
||||
if n <= 1 {
|
||||
return 0
|
||||
@ -106,7 +106,7 @@ func logRecur(n: Int) -> Int {
|
||||
return logRecur(n: n / 2) + 1
|
||||
}
|
||||
|
||||
// 线性对数阶
|
||||
/* 线性对数阶 */
|
||||
func linearLogRecur(n: Double) -> Int {
|
||||
if n <= 1 {
|
||||
return 1
|
||||
@ -118,7 +118,7 @@ func linearLogRecur(n: Double) -> Int {
|
||||
return count
|
||||
}
|
||||
|
||||
// 阶乘阶(递归实现)
|
||||
/* 阶乘阶(递归实现) */
|
||||
func factorialRecur(n: Int) -> Int {
|
||||
if n == 0 {
|
||||
return 1
|
||||
@ -133,6 +133,7 @@ func factorialRecur(n: Int) -> Int {
|
||||
|
||||
@main
|
||||
enum TimeComplexity {
|
||||
/* Driver Code */
|
||||
static func main() {
|
||||
// 可以修改 n 运行,体会一下各种复杂度的操作数量变化趋势
|
||||
let n = 8
|
||||
|
||||
@ -4,7 +4,7 @@
|
||||
* Author: nuomi1 (nuomi1@qq.com)
|
||||
*/
|
||||
|
||||
// 生成一个数组,元素为 { 1, 2, ..., n },顺序被打乱
|
||||
/* 生成一个数组,元素为 { 1, 2, ..., n },顺序被打乱 */
|
||||
func randomNumbers(n: Int) -> [Int] {
|
||||
// 生成数组 nums = { 1, 2, 3, ..., n }
|
||||
var nums = Array(1 ... n)
|
||||
@ -13,7 +13,7 @@ func randomNumbers(n: Int) -> [Int] {
|
||||
return nums
|
||||
}
|
||||
|
||||
// 查找数组 nums 中数字 1 所在索引
|
||||
/* 查找数组 nums 中数字 1 所在索引 */
|
||||
func findOne(nums: [Int]) -> Int {
|
||||
for i in nums.indices {
|
||||
if nums[i] == 1 {
|
||||
@ -25,7 +25,7 @@ func findOne(nums: [Int]) -> Int {
|
||||
|
||||
@main
|
||||
enum WorstBestTimeComplexity {
|
||||
// Driver Code
|
||||
/* Driver Code */
|
||||
static func main() {
|
||||
for _ in 0 ..< 10 {
|
||||
let n = 100
|
||||
|
||||
Reference in New Issue
Block a user