diff --git a/chapter_array_and_linkedlist/list.md b/chapter_array_and_linkedlist/list.md index 7897e5df8..c1282e51b 100755 --- a/chapter_array_and_linkedlist/list.md +++ b/chapter_array_and_linkedlist/list.md @@ -1517,6 +1517,15 @@ comments: true // 更新列表容量 _capacity = nums.count } + + /* 将列表转换为数组 */ + func toArray() -> [Int] { + var nums = Array(repeating: 0, count: _size) + for i in 0 ..< _size { + nums[i] = get(index: i) + } + return nums + } } ``` diff --git a/chapter_computational_complexity/space_complexity.md b/chapter_computational_complexity/space_complexity.md index 885051f4c..2b44ef185 100755 --- a/chapter_computational_complexity/space_complexity.md +++ b/chapter_computational_complexity/space_complexity.md @@ -1342,12 +1342,14 @@ $$ ```swift title="space_complexity.swift" /* 平方阶(递归实现) */ + @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) } ``` diff --git a/chapter_computational_complexity/space_time_tradeoff.md b/chapter_computational_complexity/space_time_tradeoff.md index b02c20043..02cadce42 100755 --- a/chapter_computational_complexity/space_time_tradeoff.md +++ b/chapter_computational_complexity/space_time_tradeoff.md @@ -161,6 +161,7 @@ comments: true === "Swift" ```swift title="leetcode_two_sum.swift" + /* 方法一:暴力枚举 */ func twoSumBruteForce(nums: [Int], target: Int) -> [Int] { // 两层循环,时间复杂度 O(n^2) for i in nums.indices.dropLast() { @@ -344,6 +345,7 @@ comments: true === "Swift" ```swift title="leetcode_two_sum.swift" + /* 方法二:辅助哈希表 */ func twoSumHashTable(nums: [Int], target: Int) -> [Int] { // 辅助哈希表,空间复杂度 O(n) var dic: [Int: Int] = [:] diff --git a/chapter_computational_complexity/time_complexity.md b/chapter_computational_complexity/time_complexity.md index 14efe36b6..afae1c035 100755 --- a/chapter_computational_complexity/time_complexity.md +++ b/chapter_computational_complexity/time_complexity.md @@ -903,7 +903,7 @@ $$ /* 常数阶 */ func constant(n: Int) -> Int { var count = 0 - let size = 100000 + let size = 100_000 for _ in 0 ..< size { count += 1 } diff --git a/chapter_hashing/hash_map.md b/chapter_hashing/hash_map.md index bc17df6e6..3f0cc3904 100755 --- a/chapter_hashing/hash_map.md +++ b/chapter_hashing/hash_map.md @@ -976,50 +976,90 @@ $$ class Entry { var key: Int var val: String - + init(key: Int, val: String) { self.key = key self.val = val } } - + /* 基于数组简易实现的哈希表 */ class ArrayHashMap { private var bucket: [Entry?] = [] - + init() { // 初始化一个长度为 100 的桶(数组) for _ in 0 ..< 100 { bucket.append(nil) } } - + /* 哈希函数 */ private func hashFunc(key: Int) -> Int { let index = key % 100 return index } - + /* 查询操作 */ func get(key: Int) -> String? { let index = hashFunc(key: key) let pair = bucket[index] return pair?.val } - + /* 添加操作 */ func put(key: Int, val: String) { let pair = Entry(key: key, val: val) let index = hashFunc(key: key) bucket[index] = pair } - + /* 删除操作 */ func remove(key: Int) { let index = hashFunc(key: key) // 置为 nil ,代表删除 bucket[index] = nil } + + /* 获取所有键值对 */ + func entrySet() -> [Entry] { + var entrySet: [Entry] = [] + for pair in bucket { + if let pair = pair { + entrySet.append(pair) + } + } + return entrySet + } + + /* 获取所有键 */ + func keySet() -> [Int] { + var keySet: [Int] = [] + for pair in bucket { + if let pair = pair { + keySet.append(pair.key) + } + } + return keySet + } + + /* 获取所有值 */ + func valueSet() -> [String] { + var valueSet: [String] = [] + for pair in bucket { + if let pair = pair { + valueSet.append(pair.val) + } + } + return valueSet + } + + /* 打印哈希表 */ + func print() { + for entry in entrySet() { + Swift.print("\(entry.key) -> \(entry.val)") + } + } } ``` diff --git a/chapter_heap/heap.md b/chapter_heap/heap.md index 64fe23b9f..59489edd8 100644 --- a/chapter_heap/heap.md +++ b/chapter_heap/heap.md @@ -386,13 +386,6 @@ comments: true === "Swift" ```swift title="my_heap.swift" - var maxHeap: [Int] - - /* 构造函数,建立空堆 */ - init() { - maxHeap = [] - } - /* 获取左子结点索引 */ func left(i: Int) -> Int { 2 * i + 1 diff --git a/chapter_sorting/quick_sort.md b/chapter_sorting/quick_sort.md index 49c32713a..e2a38564e 100755 --- a/chapter_sorting/quick_sort.md +++ b/chapter_sorting/quick_sort.md @@ -141,14 +141,14 @@ comments: true ```javascript title="quick_sort.js" /* 元素交换 */ - function swap(nums, i, j) { + swap(nums, i, j) { let tmp = nums[i]; nums[i] = nums[j]; nums[j] = tmp; } - + /* 哨兵划分 */ - function partition(nums, left, right) { + partition(nums, left, right) { // 以 nums[left] 作为基准数 let i = left, j = right; while (i < j) { @@ -159,9 +159,9 @@ comments: true i += 1; // 从左向右找首个大于基准数的元素 } // 元素交换 - swap(nums, i, j); // 交换这两个元素 + this.swap(nums, i, j); // 交换这两个元素 } - swap(nums, i, left); // 将基准数交换至两子数组的分界线 + this.swap(nums, i, left); // 将基准数交换至两子数组的分界线 return i; // 返回基准数的索引 } ``` @@ -170,14 +170,14 @@ comments: true ```typescript title="quick_sort.ts" /* 元素交换 */ - function swap(nums: number[], i: number, j: number): void { + swap(nums: number[], i: number, j: number): void { let tmp = nums[i]; nums[i] = nums[j]; nums[j] = tmp; } /* 哨兵划分 */ - function partition(nums: number[], left: number, right: number): number { + partition(nums: number[], left: number, right: number): number { // 以 nums[left] 作为基准数 let i = left, j = right; while (i < j) { @@ -188,9 +188,9 @@ comments: true i += 1; // 从左向右找首个大于基准数的元素 } // 元素交换 - swap(nums, i, j); // 交换这两个元素 + this.swap(nums, i, j); // 交换这两个元素 } - swap(nums, i, left); // 将基准数交换至两子数组的分界线 + this.swap(nums, i, left); // 将基准数交换至两子数组的分界线 return i; // 返回基准数的索引 } ``` @@ -698,13 +698,12 @@ comments: true } /* 哨兵划分(三数取中值) */ - func partition(nums: inout [Int], left: Int, right: Int) -> Int { + func partitionMedian(nums: inout [Int], left: Int, right: Int) -> Int { // 选取三个候选元素的中位数 let med = medianThree(nums: nums, left: left, mid: (left + right) / 2, right: right) // 将中位数交换至数组最左端 swap(nums: &nums, i: left, j: med) - // 以 nums[left] 作为基准数 - // 下同省略... + return partition(nums: &nums, left: left, right: right) } ``` @@ -879,7 +878,7 @@ comments: true ```swift title="quick_sort.swift" /* 快速排序(尾递归优化) */ - func quickSort(nums: inout [Int], left: Int, right: Int) { + func quickSortTailCall(nums: inout [Int], left: Int, right: Int) { var left = left var right = right // 子数组长度为 1 时终止 @@ -888,10 +887,10 @@ comments: true let pivot = partition(nums: &nums, left: left, right: right) // 对两个子数组中较短的那个执行快排 if (pivot - left) < (right - pivot) { - quickSort(nums: &nums, left: left, right: pivot - 1) // 递归排序左子数组 + quickSortTailCall(nums: &nums, left: left, right: pivot - 1) // 递归排序左子数组 left = pivot + 1 // 剩余待排序区间为 [pivot + 1, right] } else { - quickSort(nums: &nums, left: pivot + 1, right: right) // 递归排序右子数组 + quickSortTailCall(nums: &nums, left: pivot + 1, right: right) // 递归排序右子数组 right = pivot - 1 // 剩余待排序区间为 [left, pivot - 1] } } diff --git a/chapter_stack_and_queue/queue.md b/chapter_stack_and_queue/queue.md index 943a7b05d..6d6eacf53 100755 --- a/chapter_stack_and_queue/queue.md +++ b/chapter_stack_and_queue/queue.md @@ -751,19 +751,19 @@ comments: true private var front: ListNode? // 头结点 private var rear: ListNode? // 尾结点 private var _size = 0 - + init() {} - + /* 获取队列的长度 */ func size() -> Int { _size } - + /* 判断队列是否为空 */ func isEmpty() -> Bool { size() == 0 } - + /* 入队 */ func push(num: Int) { // 尾结点后添加 num @@ -780,7 +780,7 @@ comments: true } _size += 1 } - + /* 出队 */ @discardableResult func poll() -> Int { @@ -790,7 +790,7 @@ comments: true _size -= 1 return num } - + /* 访问队首元素 */ func peek() -> Int { if isEmpty() { @@ -798,6 +798,17 @@ comments: true } return front!.val } + + /* 将链表转化为 Array 并返回 */ + func toArray() -> [Int] { + var node = front + var res = Array(repeating: 0, count: size()) + for i in res.indices { + res[i] = node!.val + node = node?.next + } + return res + } } ``` @@ -1380,6 +1391,16 @@ comments: true } return nums[front] } + + /* 返回数组 */ + func toArray() -> [Int] { + // 仅转换有效长度范围内的列表元素 + var res = Array(repeating: 0, count: queSize) + for (i, j) in sequence(first: (0, front), next: { $0 < self.queSize - 1 ? ($0 + 1, $1 + 1) : nil }) { + res[i] = nums[j % capacity()] + } + return res + } } ``` diff --git a/chapter_stack_and_queue/stack.md b/chapter_stack_and_queue/stack.md index c85c325ad..9edbd6936 100755 --- a/chapter_stack_and_queue/stack.md +++ b/chapter_stack_and_queue/stack.md @@ -687,19 +687,19 @@ comments: true class LinkedListStack { private var _peek: ListNode? // 将头结点作为栈顶 private var _size = 0 // 栈的长度 - + init() {} - + /* 获取栈的长度 */ func size() -> Int { _size } - + /* 判断栈是否为空 */ func isEmpty() -> Bool { size() == 0 } - + /* 入栈 */ func push(num: Int) { let node = ListNode(x: num) @@ -707,7 +707,7 @@ comments: true _peek = node _size += 1 } - + /* 出栈 */ @discardableResult func pop() -> Int { @@ -716,7 +716,7 @@ comments: true _size -= 1 return num } - + /* 访问栈顶元素 */ func peek() -> Int { if isEmpty() { @@ -724,6 +724,17 @@ comments: true } return _peek!.val } + + /* 将 List 转化为 Array 并返回 */ + func toArray() -> [Int] { + var node = _peek + var res = Array(repeating: 0, count: _size) + for i in sequence(first: res.count - 1, next: { $0 >= 0 + 1 ? $0 - 1 : nil }) { + res[i] = node!.val + node = node?.next + } + return res + } } ``` @@ -1076,27 +1087,27 @@ comments: true /* 基于数组实现的栈 */ class ArrayStack { private var stack: [Int] - + init() { // 初始化列表(动态数组) stack = [] } - + /* 获取栈的长度 */ func size() -> Int { stack.count } - + /* 判断栈是否为空 */ func isEmpty() -> Bool { stack.isEmpty } - + /* 入栈 */ func push(num: Int) { stack.append(num) } - + /* 出栈 */ @discardableResult func pop() -> Int { @@ -1105,7 +1116,7 @@ comments: true } return stack.removeLast() } - + /* 访问栈顶元素 */ func peek() -> Int { if isEmpty() { @@ -1113,6 +1124,11 @@ comments: true } return stack.last! } + + /* 将 List 转化为 Array 并返回 */ + func toArray() -> [Int] { + stack + } } ``` diff --git a/chapter_tree/avl_tree.md b/chapter_tree/avl_tree.md index 433fa3134..4ce0e77a3 100755 --- a/chapter_tree/avl_tree.md +++ b/chapter_tree/avl_tree.md @@ -773,7 +773,6 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影 // 返回旋转后子树的根结点 return child } - ``` === "Zig"