mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-11 23:16:07 +00:00
build
This commit is contained in:
@@ -745,6 +745,11 @@ In most programming languages, we can traverse an array either by using indices
|
||||
for num in nums {
|
||||
count += num
|
||||
}
|
||||
// 同时遍历数据索引和元素
|
||||
for (i, num) in nums.enumerated() {
|
||||
count += nums[i]
|
||||
count += num
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
@@ -639,7 +639,6 @@ It's important to note that even though node `P` continues to point to `n1` afte
|
||||
let P = n0.next
|
||||
let n1 = P?.next
|
||||
n0.next = n1
|
||||
P?.next = nil
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
@@ -1382,12 +1382,15 @@ To enhance our understanding of how lists work, we will attempt to implement a s
|
||||
/* 列表类 */
|
||||
class MyList {
|
||||
private var arr: [Int] // 数组(存储列表元素)
|
||||
private var _capacity = 10 // 列表容量
|
||||
private var _size = 0 // 列表长度(当前元素数量)
|
||||
private let extendRatio = 2 // 每次列表扩容的倍数
|
||||
private var _capacity: Int // 列表容量
|
||||
private var _size: Int // 列表长度(当前元素数量)
|
||||
private let extendRatio: Int // 每次列表扩容的倍数
|
||||
|
||||
/* 构造方法 */
|
||||
init() {
|
||||
_capacity = 10
|
||||
_size = 0
|
||||
extendRatio = 2
|
||||
arr = Array(repeating: 0, count: _capacity)
|
||||
}
|
||||
|
||||
@@ -1404,7 +1407,7 @@ To enhance our understanding of how lists work, we will attempt to implement a s
|
||||
/* 访问元素 */
|
||||
func get(index: Int) -> Int {
|
||||
// 索引如果越界则抛出错误,下同
|
||||
if index < 0 || index >= _size {
|
||||
if index < 0 || index >= size() {
|
||||
fatalError("索引越界")
|
||||
}
|
||||
return arr[index]
|
||||
@@ -1412,7 +1415,7 @@ To enhance our understanding of how lists work, we will attempt to implement a s
|
||||
|
||||
/* 更新元素 */
|
||||
func set(index: Int, num: Int) {
|
||||
if index < 0 || index >= _size {
|
||||
if index < 0 || index >= size() {
|
||||
fatalError("索引越界")
|
||||
}
|
||||
arr[index] = num
|
||||
@@ -1421,25 +1424,25 @@ To enhance our understanding of how lists work, we will attempt to implement a s
|
||||
/* 在尾部添加元素 */
|
||||
func add(num: Int) {
|
||||
// 元素数量超出容量时,触发扩容机制
|
||||
if _size == _capacity {
|
||||
if size() == capacity() {
|
||||
extendCapacity()
|
||||
}
|
||||
arr[_size] = num
|
||||
arr[size()] = num
|
||||
// 更新元素数量
|
||||
_size += 1
|
||||
}
|
||||
|
||||
/* 在中间插入元素 */
|
||||
func insert(index: Int, num: Int) {
|
||||
if index < 0 || index >= _size {
|
||||
if index < 0 || index >= size() {
|
||||
fatalError("索引越界")
|
||||
}
|
||||
// 元素数量超出容量时,触发扩容机制
|
||||
if _size == _capacity {
|
||||
if size() == capacity() {
|
||||
extendCapacity()
|
||||
}
|
||||
// 将索引 index 以及之后的元素都向后移动一位
|
||||
for j in sequence(first: _size - 1, next: { $0 >= index + 1 ? $0 - 1 : nil }) {
|
||||
for j in (index ..< size()).reversed() {
|
||||
arr[j + 1] = arr[j]
|
||||
}
|
||||
arr[index] = num
|
||||
@@ -1450,12 +1453,12 @@ To enhance our understanding of how lists work, we will attempt to implement a s
|
||||
/* 删除元素 */
|
||||
@discardableResult
|
||||
func remove(index: Int) -> Int {
|
||||
if index < 0 || index >= _size {
|
||||
if index < 0 || index >= size() {
|
||||
fatalError("索引越界")
|
||||
}
|
||||
let num = arr[index]
|
||||
// 将将索引 index 之后的元素都向前移动一位
|
||||
for j in index ..< (_size - 1) {
|
||||
for j in index ..< (size() - 1) {
|
||||
arr[j] = arr[j + 1]
|
||||
}
|
||||
// 更新元素数量
|
||||
@@ -1467,18 +1470,14 @@ To enhance our understanding of how lists work, we will attempt to implement a s
|
||||
/* 列表扩容 */
|
||||
func extendCapacity() {
|
||||
// 新建一个长度为原数组 extendRatio 倍的新数组,并将原数组复制到新数组
|
||||
arr = arr + Array(repeating: 0, count: _capacity * (extendRatio - 1))
|
||||
arr = arr + Array(repeating: 0, count: capacity() * (extendRatio - 1))
|
||||
// 更新列表容量
|
||||
_capacity = arr.count
|
||||
}
|
||||
|
||||
/* 将列表转换为数组 */
|
||||
func toArray() -> [Int] {
|
||||
var arr = Array(repeating: 0, count: _size)
|
||||
for i in 0 ..< _size {
|
||||
arr[i] = get(index: i)
|
||||
}
|
||||
return arr
|
||||
Array(arr.prefix(size()))
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@@ -1631,7 +1631,7 @@ Therefore, **we can use an explicit stack to simulate the behavior of the call s
|
||||
var stack: [Int] = []
|
||||
var res = 0
|
||||
// 递:递归调用
|
||||
for i in stride(from: n, to: 0, by: -1) {
|
||||
for i in (1 ... n).reversed() {
|
||||
// 通过“入栈操作”模拟“递”
|
||||
stack.append(i)
|
||||
}
|
||||
|
||||
@@ -321,7 +321,7 @@ Let's understand this concept of "time growth trend" with an example. Assume the
|
||||
|
||||
// Time complexity of algorithm C: constant order
|
||||
func algorithmC(n: Int) {
|
||||
for _ in 0 ..< 1000000 {
|
||||
for _ in 0 ..< 1_000_000 {
|
||||
print(0)
|
||||
}
|
||||
}
|
||||
@@ -1780,7 +1780,7 @@ For instance, in bubble sort, the outer loop runs $n - 1$ times, and the inner l
|
||||
func bubbleSort(nums: inout [Int]) -> Int {
|
||||
var count = 0 // 计数器
|
||||
// 外循环:未排序区间为 [0, i]
|
||||
for i in stride(from: nums.count - 1, to: 0, by: -1) {
|
||||
for i in nums.indices.dropFirst().reversed() {
|
||||
// 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
|
||||
for j in 0 ..< i {
|
||||
if nums[j] > nums[j + 1] {
|
||||
|
||||
@@ -109,10 +109,10 @@ In other words, **basic data types provide the "content type" of data, while dat
|
||||
|
||||
```swift title=""
|
||||
// Using various basic data types to initialize arrays
|
||||
let numbers = Array(repeating: Int(), count: 5)
|
||||
let decimals = Array(repeating: Double(), count: 5)
|
||||
let characters = Array(repeating: Character("a"), count: 5)
|
||||
let bools = Array(repeating: Bool(), count: 5)
|
||||
let numbers = Array(repeating: 0, count: 5)
|
||||
let decimals = Array(repeating: 0.0, count: 5)
|
||||
let characters: [Character] = Array(repeating: "a", count: 5)
|
||||
let bools = Array(repeating: false, count: 5)
|
||||
```
|
||||
|
||||
=== "JS"
|
||||
|
||||
@@ -618,7 +618,7 @@ The code below provides a simple implementation of a separate chaining hash tabl
|
||||
|
||||
/* 负载因子 */
|
||||
func loadFactor() -> Double {
|
||||
Double(size / capacity)
|
||||
Double(size) / Double(capacity)
|
||||
}
|
||||
|
||||
/* 查询操作 */
|
||||
@@ -664,9 +664,10 @@ The code below provides a simple implementation of a separate chaining hash tabl
|
||||
for (pairIndex, pair) in bucket.enumerated() {
|
||||
if pair.key == key {
|
||||
buckets[index].remove(at: pairIndex)
|
||||
size -= 1
|
||||
break
|
||||
}
|
||||
}
|
||||
size -= 1
|
||||
}
|
||||
|
||||
/* 扩容哈希表 */
|
||||
@@ -2004,7 +2005,7 @@ The code below implements an open addressing (linear probing) hash table with la
|
||||
|
||||
/* 负载因子 */
|
||||
func loadFactor() -> Double {
|
||||
Double(size / capacity)
|
||||
Double(size) / Double(capacity)
|
||||
}
|
||||
|
||||
/* 搜索 key 对应的桶索引 */
|
||||
|
||||
@@ -983,13 +983,11 @@ The following code implements a simple hash table. Here, we encapsulate `key` an
|
||||
|
||||
/* 基于数组实现的哈希表 */
|
||||
class ArrayHashMap {
|
||||
private var buckets: [Pair?] = []
|
||||
private var buckets: [Pair?]
|
||||
|
||||
init() {
|
||||
// 初始化数组,包含 100 个桶
|
||||
for _ in 0 ..< 100 {
|
||||
buckets.append(nil)
|
||||
}
|
||||
buckets = Array(repeating: nil, count: 100)
|
||||
}
|
||||
|
||||
/* 哈希函数 */
|
||||
@@ -1021,35 +1019,17 @@ The following code implements a simple hash table. Here, we encapsulate `key` an
|
||||
|
||||
/* 获取所有键值对 */
|
||||
func pairSet() -> [Pair] {
|
||||
var pairSet: [Pair] = []
|
||||
for pair in buckets {
|
||||
if let pair = pair {
|
||||
pairSet.append(pair)
|
||||
}
|
||||
}
|
||||
return pairSet
|
||||
buckets.compactMap { $0 }
|
||||
}
|
||||
|
||||
/* 获取所有键 */
|
||||
func keySet() -> [Int] {
|
||||
var keySet: [Int] = []
|
||||
for pair in buckets {
|
||||
if let pair = pair {
|
||||
keySet.append(pair.key)
|
||||
}
|
||||
}
|
||||
return keySet
|
||||
buckets.compactMap { $0?.key }
|
||||
}
|
||||
|
||||
/* 获取所有值 */
|
||||
func valueSet() -> [String] {
|
||||
var valueSet: [String] = []
|
||||
for pair in buckets {
|
||||
if let pair = pair {
|
||||
valueSet.append(pair.val)
|
||||
}
|
||||
}
|
||||
return valueSet
|
||||
buckets.compactMap { $0?.val }
|
||||
}
|
||||
|
||||
/* 打印哈希表 */
|
||||
|
||||
@@ -999,15 +999,15 @@ The implementation code is as follows:
|
||||
class LinkedListDeque {
|
||||
private var front: ListNode? // 头节点 front
|
||||
private var rear: ListNode? // 尾节点 rear
|
||||
private var queSize: Int // 双向队列的长度
|
||||
private var _size: Int // 双向队列的长度
|
||||
|
||||
init() {
|
||||
queSize = 0
|
||||
_size = 0
|
||||
}
|
||||
|
||||
/* 获取双向队列的长度 */
|
||||
func size() -> Int {
|
||||
queSize
|
||||
_size
|
||||
}
|
||||
|
||||
/* 判断双向队列是否为空 */
|
||||
@@ -1037,7 +1037,7 @@ The implementation code is as follows:
|
||||
node.prev = rear
|
||||
rear = node // 更新尾节点
|
||||
}
|
||||
queSize += 1 // 更新队列长度
|
||||
_size += 1 // 更新队列长度
|
||||
}
|
||||
|
||||
/* 队首入队 */
|
||||
@@ -1078,7 +1078,7 @@ The implementation code is as follows:
|
||||
}
|
||||
rear = rPrev // 更新尾节点
|
||||
}
|
||||
queSize -= 1 // 更新队列长度
|
||||
_size -= 1 // 更新队列长度
|
||||
return val
|
||||
}
|
||||
|
||||
@@ -1093,13 +1093,19 @@ The implementation code is as follows:
|
||||
}
|
||||
|
||||
/* 访问队首元素 */
|
||||
func peekFirst() -> Int? {
|
||||
isEmpty() ? nil : front?.val
|
||||
func peekFirst() -> Int {
|
||||
if isEmpty() {
|
||||
fatalError("双向队列为空")
|
||||
}
|
||||
return front!.val
|
||||
}
|
||||
|
||||
/* 访问队尾元素 */
|
||||
func peekLast() -> Int? {
|
||||
isEmpty() ? nil : rear?.val
|
||||
func peekLast() -> Int {
|
||||
if isEmpty() {
|
||||
fatalError("双向队列为空")
|
||||
}
|
||||
return rear!.val
|
||||
}
|
||||
|
||||
/* 返回数组用于打印 */
|
||||
|
||||
@@ -684,9 +684,11 @@ Below is the code for implementing a queue using a linked list:
|
||||
class LinkedListQueue {
|
||||
private var front: ListNode? // 头节点
|
||||
private var rear: ListNode? // 尾节点
|
||||
private var _size = 0
|
||||
private var _size: Int
|
||||
|
||||
init() {}
|
||||
init() {
|
||||
_size = 0
|
||||
}
|
||||
|
||||
/* 获取队列的长度 */
|
||||
func size() -> Int {
|
||||
@@ -1605,12 +1607,14 @@ In a circular array, `front` or `rear` needs to loop back to the start of the ar
|
||||
/* 基于环形数组实现的队列 */
|
||||
class ArrayQueue {
|
||||
private var nums: [Int] // 用于存储队列元素的数组
|
||||
private var front = 0 // 队首指针,指向队首元素
|
||||
private var queSize = 0 // 队列长度
|
||||
private var front: Int // 队首指针,指向队首元素
|
||||
private var _size: Int // 队列长度
|
||||
|
||||
init(capacity: Int) {
|
||||
// 初始化数组
|
||||
nums = Array(repeating: 0, count: capacity)
|
||||
front = 0
|
||||
_size = 0
|
||||
}
|
||||
|
||||
/* 获取队列的容量 */
|
||||
@@ -1620,12 +1624,12 @@ In a circular array, `front` or `rear` needs to loop back to the start of the ar
|
||||
|
||||
/* 获取队列的长度 */
|
||||
func size() -> Int {
|
||||
queSize
|
||||
_size
|
||||
}
|
||||
|
||||
/* 判断队列是否为空 */
|
||||
func isEmpty() -> Bool {
|
||||
queSize == 0
|
||||
size() == 0
|
||||
}
|
||||
|
||||
/* 入队 */
|
||||
@@ -1636,10 +1640,10 @@ In a circular array, `front` or `rear` needs to loop back to the start of the ar
|
||||
}
|
||||
// 计算队尾指针,指向队尾索引 + 1
|
||||
// 通过取余操作实现 rear 越过数组尾部后回到头部
|
||||
let rear = (front + queSize) % capacity()
|
||||
let rear = (front + size()) % capacity()
|
||||
// 将 num 添加至队尾
|
||||
nums[rear] = num
|
||||
queSize += 1
|
||||
_size += 1
|
||||
}
|
||||
|
||||
/* 出队 */
|
||||
@@ -1648,7 +1652,7 @@ In a circular array, `front` or `rear` needs to loop back to the start of the ar
|
||||
let num = peek()
|
||||
// 队首指针向后移动一位,若越过尾部,则返回到数组头部
|
||||
front = (front + 1) % capacity()
|
||||
queSize -= 1
|
||||
_size -= 1
|
||||
return num
|
||||
}
|
||||
|
||||
@@ -1663,11 +1667,7 @@ In a circular array, `front` or `rear` needs to loop back to the start of the ar
|
||||
/* 返回数组 */
|
||||
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
|
||||
(front ..< front + size()).map { nums[$0 % capacity()] }
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@@ -643,9 +643,11 @@ Below is an example code for implementing a stack based on a linked list:
|
||||
/* 基于链表实现的栈 */
|
||||
class LinkedListStack {
|
||||
private var _peek: ListNode? // 将头节点作为栈顶
|
||||
private var _size = 0 // 栈的长度
|
||||
private var _size: Int // 栈的长度
|
||||
|
||||
init() {}
|
||||
init() {
|
||||
_size = 0
|
||||
}
|
||||
|
||||
/* 获取栈的长度 */
|
||||
func size() -> Int {
|
||||
@@ -685,8 +687,8 @@ Below is an example code for implementing a stack based on a linked list:
|
||||
/* 将 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 }) {
|
||||
var res = Array(repeating: 0, count: size())
|
||||
for i in res.indices.reversed() {
|
||||
res[i] = node!.val
|
||||
node = node?.next
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user