mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 04:31:55 +08:00
feat: Revised the book (#978)
* Sync recent changes to the revised Word. * Revised the preface chapter * Revised the introduction chapter * Revised the computation complexity chapter * Revised the chapter data structure * Revised the chapter array and linked list * Revised the chapter stack and queue * Revised the chapter hashing * Revised the chapter tree * Revised the chapter heap * Revised the chapter graph * Revised the chapter searching * Reivised the sorting chapter * Revised the divide and conquer chapter * Revised the chapter backtacking * Revised the DP chapter * Revised the greedy chapter * Revised the appendix chapter * Revised the preface chapter doubly * Revised the figures
This commit is contained in:
@ -31,11 +31,11 @@ func insert(nums: inout [Int], num: Int, index: Int) {
|
||||
for i in nums.indices.dropFirst(index).reversed() {
|
||||
nums[i] = nums[i - 1]
|
||||
}
|
||||
// 将 num 赋给 index 处元素
|
||||
// 将 num 赋给 index 处的元素
|
||||
nums[index] = num
|
||||
}
|
||||
|
||||
/* 删除索引 index 处元素 */
|
||||
/* 删除索引 index 处的元素 */
|
||||
func remove(nums: inout [Int], index: Int) {
|
||||
// 把索引 index 之后的所有元素向前移动一位
|
||||
for i in nums.indices.dropFirst(index).dropLast() {
|
||||
|
||||
@ -62,7 +62,7 @@ enum LinkedList {
|
||||
let n2 = ListNode(x: 2)
|
||||
let n3 = ListNode(x: 5)
|
||||
let n4 = ListNode(x: 4)
|
||||
// 构建引用指向
|
||||
// 构建节点之间的引用
|
||||
n0.next = n1
|
||||
n1.next = n2
|
||||
n2.next = n3
|
||||
|
||||
@ -24,7 +24,7 @@ enum List {
|
||||
nums.removeAll()
|
||||
print("清空列表后 nums = \(nums)")
|
||||
|
||||
/* 尾部添加元素 */
|
||||
/* 在尾部添加元素 */
|
||||
nums.append(1)
|
||||
nums.append(3)
|
||||
nums.append(2)
|
||||
@ -32,7 +32,7 @@ enum List {
|
||||
nums.append(4)
|
||||
print("添加元素后 nums = \(nums)")
|
||||
|
||||
/* 中间插入元素 */
|
||||
/* 在中间插入元素 */
|
||||
nums.insert(6, at: 3)
|
||||
print("在索引 3 处插入数字 6 ,得到 nums = \(nums)")
|
||||
|
||||
|
||||
@ -4,11 +4,11 @@
|
||||
* Author: nuomi1 (nuomi1@qq.com)
|
||||
*/
|
||||
|
||||
/* 列表类简易实现 */
|
||||
/* 列表类 */
|
||||
class MyList {
|
||||
private var arr: [Int] // 数组(存储列表元素)
|
||||
private var _capacity = 10 // 列表容量
|
||||
private var _size = 0 // 列表长度(即当前元素数量)
|
||||
private var _size = 0 // 列表长度(当前元素数量)
|
||||
private let extendRatio = 2 // 每次列表扩容的倍数
|
||||
|
||||
/* 构造方法 */
|
||||
@ -16,7 +16,7 @@ class MyList {
|
||||
arr = Array(repeating: 0, count: _capacity)
|
||||
}
|
||||
|
||||
/* 获取列表长度(即当前元素数量)*/
|
||||
/* 获取列表长度(当前元素数量)*/
|
||||
func size() -> Int {
|
||||
_size
|
||||
}
|
||||
@ -43,7 +43,7 @@ class MyList {
|
||||
arr[index] = num
|
||||
}
|
||||
|
||||
/* 尾部添加元素 */
|
||||
/* 在尾部添加元素 */
|
||||
func add(num: Int) {
|
||||
// 元素数量超出容量时,触发扩容机制
|
||||
if _size == _capacity {
|
||||
@ -54,7 +54,7 @@ class MyList {
|
||||
_size += 1
|
||||
}
|
||||
|
||||
/* 中间插入元素 */
|
||||
/* 在中间插入元素 */
|
||||
func insert(index: Int, num: Int) {
|
||||
if index < 0 || index >= _size {
|
||||
fatalError("索引越界")
|
||||
@ -113,7 +113,7 @@ enum _MyList {
|
||||
static func main() {
|
||||
/* 初始化列表 */
|
||||
let nums = MyList()
|
||||
/* 尾部添加元素 */
|
||||
/* 在尾部添加元素 */
|
||||
nums.add(num: 1)
|
||||
nums.add(num: 3)
|
||||
nums.add(num: 2)
|
||||
@ -121,7 +121,7 @@ enum _MyList {
|
||||
nums.add(num: 4)
|
||||
print("列表 nums = \(nums.toArray()) ,容量 = \(nums.capacity()) ,长度 = \(nums.size())")
|
||||
|
||||
/* 中间插入元素 */
|
||||
/* 在中间插入元素 */
|
||||
nums.insert(index: 3, num: 6)
|
||||
print("在索引 3 处插入数字 6 ,得到 nums = \(nums.toArray())")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user