mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-16 21:50:59 +00:00
build
This commit is contained in:
@@ -1200,7 +1200,7 @@ Traverse the linked list to locate a node whose value matches `target`, and then
|
||||
var index = 0
|
||||
var h = head
|
||||
while (h != null) {
|
||||
if (h.value == target)
|
||||
if (h._val == target)
|
||||
return index
|
||||
h = h.next
|
||||
index++
|
||||
|
||||
@@ -2175,7 +2175,7 @@ To enhance our understanding of how lists work, we will attempt to implement a s
|
||||
# 元素数量超出容量时,触发扩容机制
|
||||
extend_capacity if size == capacity
|
||||
@arr[size] = num
|
||||
|
||||
|
||||
# 更新元素数量
|
||||
@size += 1
|
||||
end
|
||||
@@ -2189,7 +2189,7 @@ To enhance our understanding of how lists work, we will attempt to implement a s
|
||||
|
||||
# 将索引 index 以及之后的元素都向后移动一位
|
||||
for j in (size - 1).downto(index)
|
||||
@arr[j + 1] = @arr[j]
|
||||
@arr[j + 1] = @arr[j]
|
||||
end
|
||||
@arr[index] = num
|
||||
|
||||
|
||||
@@ -1066,10 +1066,10 @@ Below is the implementation code for graphs represented using an adjacency matri
|
||||
}
|
||||
|
||||
/* 添加顶点 */
|
||||
fun addVertex(value: Int) {
|
||||
fun addVertex(_val: Int) {
|
||||
val n = size()
|
||||
// 向顶点列表中添加新顶点的值
|
||||
vertices.add(value)
|
||||
vertices.add(_val)
|
||||
// 在邻接矩阵中添加一行
|
||||
val newRow = mutableListOf<Int>()
|
||||
for (j in 0..<n) {
|
||||
@@ -2222,9 +2222,9 @@ Additionally, we use the `Vertex` class to represent vertices in the adjacency l
|
||||
for (pair in adjList.entries) {
|
||||
val tmp = mutableListOf<Int>()
|
||||
for (vertex in pair.value) {
|
||||
tmp.add(vertex.value)
|
||||
tmp.add(vertex._val)
|
||||
}
|
||||
println("${pair.key.value}: $tmp,")
|
||||
println("${pair.key._val}: $tmp,")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1347,14 +1347,14 @@ The code below provides a simple implementation of a separate chaining hash tabl
|
||||
val bucket = buckets[index]
|
||||
// 遍历桶,若找到 key ,则返回对应 val
|
||||
for (pair in bucket) {
|
||||
if (pair.key == key) return pair.value
|
||||
if (pair.key == key) return pair._val
|
||||
}
|
||||
// 若未找到 key ,则返回 null
|
||||
return null
|
||||
}
|
||||
|
||||
/* 添加操作 */
|
||||
fun put(key: Int, value: String) {
|
||||
fun put(key: Int, _val: String) {
|
||||
// 当负载因子超过阈值时,执行扩容
|
||||
if (loadFactor() > loadThres) {
|
||||
extend()
|
||||
@@ -1364,12 +1364,12 @@ The code below provides a simple implementation of a separate chaining hash tabl
|
||||
// 遍历桶,若遇到指定 key ,则更新对应 val 并返回
|
||||
for (pair in bucket) {
|
||||
if (pair.key == key) {
|
||||
pair.value = value
|
||||
pair._val = _val
|
||||
return
|
||||
}
|
||||
}
|
||||
// 若无该 key ,则将键值对添加至尾部
|
||||
val pair = Pair(key, value)
|
||||
val pair = Pair(key, _val)
|
||||
bucket.add(pair)
|
||||
size++
|
||||
}
|
||||
@@ -1403,7 +1403,7 @@ The code below provides a simple implementation of a separate chaining hash tabl
|
||||
// 将键值对从原哈希表搬运至新哈希表
|
||||
for (bucket in bucketsTmp) {
|
||||
for (pair in bucket) {
|
||||
put(pair.key, pair.value)
|
||||
put(pair.key, pair._val)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1414,7 +1414,7 @@ The code below provides a simple implementation of a separate chaining hash tabl
|
||||
val res = mutableListOf<String>()
|
||||
for (pair in bucket) {
|
||||
val k = pair.key
|
||||
val v = pair.value
|
||||
val v = pair._val
|
||||
res.add("$k -> $v")
|
||||
}
|
||||
println(res)
|
||||
@@ -3017,14 +3017,14 @@ The code below implements an open addressing (linear probing) hash table with la
|
||||
val index = findBucket(key)
|
||||
// 若找到键值对,则返回对应 val
|
||||
if (buckets[index] != null && buckets[index] != TOMBSTONE) {
|
||||
return buckets[index]?.value
|
||||
return buckets[index]?._val
|
||||
}
|
||||
// 若键值对不存在,则返回 null
|
||||
return null
|
||||
}
|
||||
|
||||
/* 添加操作 */
|
||||
fun put(key: Int, value: String) {
|
||||
fun put(key: Int, _val: String) {
|
||||
// 当负载因子超过阈值时,执行扩容
|
||||
if (loadFactor() > loadThres) {
|
||||
extend()
|
||||
@@ -3033,11 +3033,11 @@ The code below implements an open addressing (linear probing) hash table with la
|
||||
val index = findBucket(key)
|
||||
// 若找到键值对,则覆盖 val 并返回
|
||||
if (buckets[index] != null && buckets[index] != TOMBSTONE) {
|
||||
buckets[index]!!.value = value
|
||||
buckets[index]!!._val = _val
|
||||
return
|
||||
}
|
||||
// 若键值对不存在,则添加该键值对
|
||||
buckets[index] = Pair(key, value)
|
||||
buckets[index] = Pair(key, _val)
|
||||
size++
|
||||
}
|
||||
|
||||
@@ -3063,7 +3063,7 @@ The code below implements an open addressing (linear probing) hash table with la
|
||||
// 将键值对从原哈希表搬运至新哈希表
|
||||
for (pair in bucketsTmp) {
|
||||
if (pair != null && pair != TOMBSTONE) {
|
||||
put(pair.key, pair.value)
|
||||
put(pair.key, pair._val)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3076,7 +3076,7 @@ The code below implements an open addressing (linear probing) hash table with la
|
||||
} else if (pair == TOMBSTONE) {
|
||||
println("TOMESTOME")
|
||||
} else {
|
||||
println("${pair.key} -> ${pair.value}")
|
||||
println("${pair.key} -> ${pair._val}")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1543,7 +1543,7 @@ The following code implements a simple hash table. Here, we encapsulate `key` an
|
||||
/* 键值对 */
|
||||
class Pair(
|
||||
var key: Int,
|
||||
var value: String
|
||||
var _val: String
|
||||
)
|
||||
|
||||
/* 基于数组实现的哈希表 */
|
||||
@@ -1561,12 +1561,12 @@ The following code implements a simple hash table. Here, we encapsulate `key` an
|
||||
fun get(key: Int): String? {
|
||||
val index = hashFunc(key)
|
||||
val pair = buckets[index] ?: return null
|
||||
return pair.value
|
||||
return pair._val
|
||||
}
|
||||
|
||||
/* 添加操作 */
|
||||
fun put(key: Int, value: String) {
|
||||
val pair = Pair(key, value)
|
||||
fun put(key: Int, _val: String) {
|
||||
val pair = Pair(key, _val)
|
||||
val index = hashFunc(key)
|
||||
buckets[index] = pair
|
||||
}
|
||||
@@ -1602,7 +1602,7 @@ The following code implements a simple hash table. Here, we encapsulate `key` an
|
||||
fun valueSet(): MutableList<String> {
|
||||
val valueSet = mutableListOf<String>()
|
||||
for (pair in buckets) {
|
||||
pair?.let { valueSet.add(it.value) }
|
||||
pair?.let { valueSet.add(it._val) }
|
||||
}
|
||||
return valueSet
|
||||
}
|
||||
@@ -1611,8 +1611,8 @@ The following code implements a simple hash table. Here, we encapsulate `key` an
|
||||
fun print() {
|
||||
for (kv in pairSet()) {
|
||||
val key = kv.key
|
||||
val value = kv.value
|
||||
println("${key} -> ${value}")
|
||||
val _val = kv._val
|
||||
println("${key} -> ${_val}")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1632,12 +1632,12 @@ The following code implements a simple hash table. Here, we encapsulate `key` an
|
||||
fun get(key: Int): String? {
|
||||
val index = hashFunc(key)
|
||||
val pair = buckets[index] ?: return null
|
||||
return pair.value
|
||||
return pair._val
|
||||
}
|
||||
|
||||
/* 添加操作 */
|
||||
fun put(key: Int, value: String) {
|
||||
val pair = Pair(key, value)
|
||||
fun put(key: Int, _val: String) {
|
||||
val pair = Pair(key, _val)
|
||||
val index = hashFunc(key)
|
||||
buckets[index] = pair
|
||||
}
|
||||
@@ -1673,7 +1673,7 @@ The following code implements a simple hash table. Here, we encapsulate `key` an
|
||||
fun valueSet(): MutableList<String> {
|
||||
val valueSet = mutableListOf<String>()
|
||||
for (pair in buckets) {
|
||||
pair?.let { valueSet.add(it.value) }
|
||||
pair?.let { valueSet.add(it._val) }
|
||||
}
|
||||
return valueSet
|
||||
}
|
||||
@@ -1682,8 +1682,8 @@ The following code implements a simple hash table. Here, we encapsulate `key` an
|
||||
fun print() {
|
||||
for (kv in pairSet()) {
|
||||
val key = kv.key
|
||||
val value = kv.value
|
||||
println("${key} -> ${value}")
|
||||
val _val = kv._val
|
||||
println("${key} -> ${_val}")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -240,9 +240,9 @@ It's worth mentioning that **since leaf nodes have no children, they naturally f
|
||||
}
|
||||
|
||||
/* 元素入堆 */
|
||||
fun push(value: Int) {
|
||||
fun push(_val: Int) {
|
||||
// 添加节点
|
||||
maxHeap.add(value)
|
||||
maxHeap.add(_val)
|
||||
// 从底至顶堆化
|
||||
siftUp(size() - 1)
|
||||
}
|
||||
@@ -270,11 +270,11 @@ It's worth mentioning that **since leaf nodes have no children, they naturally f
|
||||
// 交换根节点与最右叶节点(交换首元素与尾元素)
|
||||
swap(0, size() - 1)
|
||||
// 删除节点
|
||||
val value = maxHeap.removeAt(size() - 1)
|
||||
val _val = maxHeap.removeAt(size() - 1)
|
||||
// 从顶至底堆化
|
||||
siftDown(0)
|
||||
// 返回堆顶元素
|
||||
return value
|
||||
return _val
|
||||
}
|
||||
|
||||
/* 从节点 i 开始,从顶至底堆化 */
|
||||
|
||||
@@ -1183,9 +1183,9 @@ Given a total of $n$ nodes, the height of the tree is $O(\log n)$. Hence, the lo
|
||||
|
||||
```kotlin title="my_heap.kt"
|
||||
/* 元素入堆 */
|
||||
fun push(value: Int) {
|
||||
fun push(_val: Int) {
|
||||
// 添加节点
|
||||
maxHeap.add(value)
|
||||
maxHeap.add(_val)
|
||||
// 从底至顶堆化
|
||||
siftUp(size() - 1)
|
||||
}
|
||||
@@ -1735,11 +1735,11 @@ Similar to the element insertion operation, the time complexity of the top eleme
|
||||
// 交换根节点与最右叶节点(交换首元素与尾元素)
|
||||
swap(0, size() - 1)
|
||||
// 删除节点
|
||||
val value = maxHeap.removeAt(size() - 1)
|
||||
val _val = maxHeap.removeAt(size() - 1)
|
||||
// 从顶至底堆化
|
||||
siftDown(0)
|
||||
// 返回堆顶元素
|
||||
return value
|
||||
return _val
|
||||
}
|
||||
|
||||
/* 从节点 i 开始,从顶至底堆化 */
|
||||
|
||||
@@ -1904,10 +1904,10 @@ The implementation code is as follows:
|
||||
fun pop(isFront: Boolean): Int {
|
||||
if (isEmpty())
|
||||
throw IndexOutOfBoundsException()
|
||||
val value: Int
|
||||
val _val: Int
|
||||
// 队首出队操作
|
||||
if (isFront) {
|
||||
value = front!!._val // 暂存头节点值
|
||||
_val = front!!._val // 暂存头节点值
|
||||
// 删除头节点
|
||||
val fNext = front!!.next
|
||||
if (fNext != null) {
|
||||
@@ -1917,7 +1917,7 @@ The implementation code is as follows:
|
||||
front = fNext // 更新头节点
|
||||
// 队尾出队操作
|
||||
} else {
|
||||
value = rear!!._val // 暂存尾节点值
|
||||
_val = rear!!._val // 暂存尾节点值
|
||||
// 删除尾节点
|
||||
val rPrev = rear!!.prev
|
||||
if (rPrev != null) {
|
||||
@@ -1927,7 +1927,7 @@ The implementation code is as follows:
|
||||
rear = rPrev // 更新尾节点
|
||||
}
|
||||
queSize-- // 更新队列长度
|
||||
return value
|
||||
return _val
|
||||
}
|
||||
|
||||
/* 队首出队 */
|
||||
|
||||
@@ -1180,7 +1180,7 @@ The following code implements a binary tree based on array representation, inclu
|
||||
}
|
||||
|
||||
/* 获取索引为 i 节点的值 */
|
||||
fun value(i: Int): Int? {
|
||||
fun _val(i: Int): Int? {
|
||||
// 若索引越界,则返回 null ,代表空位
|
||||
if (i < 0 || i >= size()) return null
|
||||
return tree[i]
|
||||
@@ -1206,8 +1206,8 @@ The following code implements a binary tree based on array representation, inclu
|
||||
val res = mutableListOf<Int?>()
|
||||
// 直接遍历数组
|
||||
for (i in 0..<size()) {
|
||||
if (value(i) != null)
|
||||
res.add(value(i))
|
||||
if (_val(i) != null)
|
||||
res.add(_val(i))
|
||||
}
|
||||
return res
|
||||
}
|
||||
@@ -1215,19 +1215,19 @@ The following code implements a binary tree based on array representation, inclu
|
||||
/* 深度优先遍历 */
|
||||
fun dfs(i: Int, order: String, res: MutableList<Int?>) {
|
||||
// 若为空位,则返回
|
||||
if (value(i) == null)
|
||||
if (_val(i) == null)
|
||||
return
|
||||
// 前序遍历
|
||||
if ("pre" == order)
|
||||
res.add(value(i))
|
||||
res.add(_val(i))
|
||||
dfs(left(i), order, res)
|
||||
// 中序遍历
|
||||
if ("in" == order)
|
||||
res.add(value(i))
|
||||
res.add(_val(i))
|
||||
dfs(right(i), order, res)
|
||||
// 后序遍历
|
||||
if ("post" == order)
|
||||
res.add(value(i))
|
||||
res.add(_val(i))
|
||||
}
|
||||
|
||||
/* 前序遍历 */
|
||||
|
||||
@@ -2012,20 +2012,20 @@ The node insertion operation in AVL trees is similar to that in binary search tr
|
||||
|
||||
```kotlin title="avl_tree.kt"
|
||||
/* 插入节点 */
|
||||
fun insert(value: Int) {
|
||||
root = insertHelper(root, value)
|
||||
fun insert(_val: Int) {
|
||||
root = insertHelper(root, _val)
|
||||
}
|
||||
|
||||
/* 递归插入节点(辅助方法) */
|
||||
fun insertHelper(n: TreeNode?, value: Int): TreeNode {
|
||||
fun insertHelper(n: TreeNode?, _val: Int): TreeNode {
|
||||
if (n == null)
|
||||
return TreeNode(value)
|
||||
return TreeNode(_val)
|
||||
var node = n
|
||||
/* 1. 查找插入位置并插入节点 */
|
||||
if (value < node.value)
|
||||
node.left = insertHelper(node.left, value)
|
||||
else if (value > node.value)
|
||||
node.right = insertHelper(node.right, value)
|
||||
if (_val < node._val)
|
||||
node.left = insertHelper(node.left, _val)
|
||||
else if (_val > node._val)
|
||||
node.right = insertHelper(node.right, _val)
|
||||
else
|
||||
return node // 重复节点不插入,直接返回
|
||||
updateHeight(node) // 更新节点高度
|
||||
@@ -2595,18 +2595,18 @@ Similarly, based on the method of removing nodes in binary search trees, rotatio
|
||||
|
||||
```kotlin title="avl_tree.kt"
|
||||
/* 删除节点 */
|
||||
fun remove(value: Int) {
|
||||
root = removeHelper(root, value)
|
||||
fun remove(_val: Int) {
|
||||
root = removeHelper(root, _val)
|
||||
}
|
||||
|
||||
/* 递归删除节点(辅助方法) */
|
||||
fun removeHelper(n: TreeNode?, value: Int): TreeNode? {
|
||||
fun removeHelper(n: TreeNode?, _val: Int): TreeNode? {
|
||||
var node = n ?: return null
|
||||
/* 1. 查找节点并删除 */
|
||||
if (value < node.value)
|
||||
node.left = removeHelper(node.left, value)
|
||||
else if (value > node.value)
|
||||
node.right = removeHelper(node.right, value)
|
||||
if (_val < node._val)
|
||||
node.left = removeHelper(node.left, _val)
|
||||
else if (_val > node._val)
|
||||
node.right = removeHelper(node.right, _val)
|
||||
else {
|
||||
if (node.left == null || node.right == null) {
|
||||
val child = if (node.left != null)
|
||||
@@ -2625,8 +2625,8 @@ Similarly, based on the method of removing nodes in binary search trees, rotatio
|
||||
while (temp!!.left != null) {
|
||||
temp = temp.left
|
||||
}
|
||||
node.right = removeHelper(node.right, temp.value)
|
||||
node.value = temp.value
|
||||
node.right = removeHelper(node.right, temp._val)
|
||||
node._val = temp._val
|
||||
}
|
||||
}
|
||||
updateHeight(node) // 更新节点高度
|
||||
|
||||
@@ -299,10 +299,10 @@ The search operation in a binary search tree works on the same principle as the
|
||||
// 循环查找,越过叶节点后跳出
|
||||
while (cur != null) {
|
||||
// 目标节点在 cur 的右子树中
|
||||
cur = if (cur.value < num)
|
||||
cur = if (cur._val < num)
|
||||
cur.right
|
||||
// 目标节点在 cur 的左子树中
|
||||
else if (cur.value > num)
|
||||
else if (cur._val > num)
|
||||
cur.left
|
||||
// 找到目标节点,跳出循环
|
||||
else
|
||||
@@ -751,11 +751,11 @@ In the code implementation, note the following two points.
|
||||
// 循环查找,越过叶节点后跳出
|
||||
while (cur != null) {
|
||||
// 找到重复节点,直接返回
|
||||
if (cur.value == num)
|
||||
if (cur._val == num)
|
||||
return
|
||||
pre = cur
|
||||
// 插入位置在 cur 的右子树中
|
||||
cur = if (cur.value < num)
|
||||
cur = if (cur._val < num)
|
||||
cur.right
|
||||
// 插入位置在 cur 的左子树中
|
||||
else
|
||||
@@ -763,7 +763,7 @@ In the code implementation, note the following two points.
|
||||
}
|
||||
// 插入节点
|
||||
val node = TreeNode(num)
|
||||
if (pre?.value!! < num)
|
||||
if (pre?._val!! < num)
|
||||
pre.right = node
|
||||
else
|
||||
pre.left = node
|
||||
@@ -1497,11 +1497,11 @@ The operation of removing a node also uses $O(\log n)$ time, where finding the n
|
||||
// 循环查找,越过叶节点后跳出
|
||||
while (cur != null) {
|
||||
// 找到待删除节点,跳出循环
|
||||
if (cur.value == num)
|
||||
if (cur._val == num)
|
||||
break
|
||||
pre = cur
|
||||
// 待删除节点在 cur 的右子树中
|
||||
cur = if (cur.value < num)
|
||||
cur = if (cur._val < num)
|
||||
cur.right
|
||||
// 待删除节点在 cur 的左子树中
|
||||
else
|
||||
@@ -1535,9 +1535,9 @@ The operation of removing a node also uses $O(\log n)$ time, where finding the n
|
||||
tmp = tmp.left
|
||||
}
|
||||
// 递归删除节点 tmp
|
||||
remove(tmp.value)
|
||||
remove(tmp._val)
|
||||
// 用 tmp 覆盖 cur
|
||||
cur.value = tmp.value
|
||||
cur._val = tmp._val
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@@ -305,7 +305,7 @@ Breadth-first traversal is usually implemented with the help of a "queue". The q
|
||||
val list = mutableListOf<Int>()
|
||||
while (queue.isNotEmpty()) {
|
||||
val node = queue.poll() // 队列出队
|
||||
list.add(node?.value!!) // 保存节点值
|
||||
list.add(node?._val!!) // 保存节点值
|
||||
if (node.left != null)
|
||||
queue.offer(node.left) // 左子节点入队
|
||||
if (node.right != null)
|
||||
@@ -764,7 +764,7 @@ Depth-first search is usually implemented based on recursion:
|
||||
fun preOrder(root: TreeNode?) {
|
||||
if (root == null) return
|
||||
// 访问优先级:根节点 -> 左子树 -> 右子树
|
||||
list.add(root.value)
|
||||
list.add(root._val)
|
||||
preOrder(root.left)
|
||||
preOrder(root.right)
|
||||
}
|
||||
@@ -774,7 +774,7 @@ Depth-first search is usually implemented based on recursion:
|
||||
if (root == null) return
|
||||
// 访问优先级:左子树 -> 根节点 -> 右子树
|
||||
inOrder(root.left)
|
||||
list.add(root.value)
|
||||
list.add(root._val)
|
||||
inOrder(root.right)
|
||||
}
|
||||
|
||||
@@ -784,7 +784,7 @@ Depth-first search is usually implemented based on recursion:
|
||||
// 访问优先级:左子树 -> 右子树 -> 根节点
|
||||
postOrder(root.left)
|
||||
postOrder(root.right)
|
||||
list.add(root.value)
|
||||
list.add(root._val)
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
Reference in New Issue
Block a user