mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-14 16:16:06 +00:00
build
This commit is contained in:
@@ -813,6 +813,7 @@ comments: true
|
||||
fun remove(n0: ListNode?) {
|
||||
if (n0?.next == null)
|
||||
return
|
||||
// n0 -> P -> n1
|
||||
val p = n0.next
|
||||
val n1 = p?.next
|
||||
n0.next = n1
|
||||
|
||||
@@ -385,10 +385,10 @@ comments: true
|
||||
nums.Add(4);
|
||||
|
||||
/* 在中间插入元素 */
|
||||
nums.Insert(3, 6);
|
||||
nums.Insert(3, 6); // 在索引 3 处插入数字 6
|
||||
|
||||
/* 删除元素 */
|
||||
nums.RemoveAt(3);
|
||||
nums.RemoveAt(3); // 删除索引 3 处的元素
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
@@ -445,10 +445,10 @@ comments: true
|
||||
nums.push(4);
|
||||
|
||||
/* 在中间插入元素 */
|
||||
nums.splice(3, 0, 6);
|
||||
nums.splice(3, 0, 6); // 在索引 3 处插入数字 6
|
||||
|
||||
/* 删除元素 */
|
||||
nums.splice(3, 1);
|
||||
nums.splice(3, 1); // 删除索引 3 处的元素
|
||||
```
|
||||
|
||||
=== "TS"
|
||||
@@ -465,10 +465,10 @@ comments: true
|
||||
nums.push(4);
|
||||
|
||||
/* 在中间插入元素 */
|
||||
nums.splice(3, 0, 6);
|
||||
nums.splice(3, 0, 6); // 在索引 3 处插入数字 6
|
||||
|
||||
/* 删除元素 */
|
||||
nums.splice(3, 1);
|
||||
nums.splice(3, 1); // 删除索引 3 处的元素
|
||||
```
|
||||
|
||||
=== "Dart"
|
||||
|
||||
@@ -1992,6 +1992,7 @@ comments: true
|
||||
var res = 0
|
||||
// 递: 递归调用
|
||||
for (i in n downTo 0) {
|
||||
// 通过“入栈操作”模拟“递”
|
||||
stack.push(i)
|
||||
}
|
||||
// 归: 返回结果
|
||||
|
||||
@@ -1431,7 +1431,6 @@ $$
|
||||
/* 线性阶 */
|
||||
fun linear(n: Int): Int {
|
||||
var count = 0
|
||||
// 循环次数与数组长度成正比
|
||||
for (i in 0..<n)
|
||||
count++
|
||||
return count
|
||||
@@ -2175,7 +2174,9 @@ $$
|
||||
for (j in 0..<i) {
|
||||
if (nums[j] > nums[j + 1]) {
|
||||
// 交换 nums[j] 与 nums[j + 1]
|
||||
nums[j] = nums[j + 1].also { nums[j + 1] = nums[j] }
|
||||
val temp = nums[j]
|
||||
nums[j] = nums[j + 1]
|
||||
nums[j + 1] = temp
|
||||
count += 3 // 元素交换包含 3 个单元操作
|
||||
}
|
||||
}
|
||||
@@ -2452,8 +2453,8 @@ $$
|
||||
/* 指数阶(循环实现) */
|
||||
fun exponential(n: Int): Int {
|
||||
var count = 0
|
||||
// 细胞每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)
|
||||
var base = 1
|
||||
// 细胞每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)
|
||||
for (i in 0..<n) {
|
||||
for (j in 0..<base) {
|
||||
count++
|
||||
@@ -3859,12 +3860,11 @@ $$
|
||||
for (i in 0..<n) {
|
||||
nums[i] = i + 1
|
||||
}
|
||||
val mutableList = nums.toMutableList()
|
||||
// 随机打乱数组元素
|
||||
mutableList.shuffle()
|
||||
nums.shuffle()
|
||||
val res = arrayOfNulls<Int>(n)
|
||||
for (i in 0..<n) {
|
||||
res[i] = mutableList[i]
|
||||
res[i] = nums[i]
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
@@ -184,7 +184,9 @@ comments: true
|
||||
if state + choice > n {
|
||||
continue
|
||||
}
|
||||
// 尝试:做出选择,更新状态
|
||||
backtrack(choices: choices, state: state + choice, n: n, res: &res)
|
||||
// 回退
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1597,7 +1599,9 @@ $$
|
||||
var a = 1
|
||||
var b = 2
|
||||
for (i in 3..n) {
|
||||
b += a.also { a = b }
|
||||
val temp = b
|
||||
b += a
|
||||
a = temp
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
@@ -1036,11 +1036,7 @@ $$
|
||||
|
||||
```kotlin title="knapsack.kt"
|
||||
/* 0-1 背包:动态规划 */
|
||||
fun knapsackDP(
|
||||
wgt: IntArray,
|
||||
_val: IntArray,
|
||||
cap: Int
|
||||
): Int {
|
||||
fun knapsackDP(wgt: IntArray, _val: IntArray, cap: Int): Int {
|
||||
val n = wgt.size
|
||||
// 初始化 dp 表
|
||||
val dp = Array(n + 1) { IntArray(cap + 1) }
|
||||
@@ -1429,11 +1425,7 @@ $$
|
||||
|
||||
```kotlin title="knapsack.kt"
|
||||
/* 0-1 背包:空间优化后的动态规划 */
|
||||
fun knapsackDPComp(
|
||||
wgt: IntArray,
|
||||
_val: IntArray,
|
||||
cap: Int
|
||||
): Int {
|
||||
fun knapsackDPComp(wgt: IntArray, _val: IntArray, cap: Int): Int {
|
||||
val n = wgt.size
|
||||
// 初始化 dp 表
|
||||
val dp = IntArray(cap + 1)
|
||||
@@ -1443,8 +1435,7 @@ $$
|
||||
for (c in cap downTo 1) {
|
||||
if (wgt[i - 1] <= c) {
|
||||
// 不选和选物品 i 这两种方案的较大值
|
||||
dp[c] =
|
||||
max(dp[c], dp[c - wgt[i - 1]] + _val[i - 1])
|
||||
dp[c] = max(dp[c], dp[c - wgt[i - 1]] + _val[i - 1])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1103,8 +1103,8 @@ comments: true
|
||||
if (i < 0 || j < 0 || i >= size() || j >= size() || i == j)
|
||||
throw IndexOutOfBoundsException()
|
||||
// 在无向图中,邻接矩阵关于主对角线对称,即满足 (i, j) == (j, i)
|
||||
adjMat[i][j] = 1;
|
||||
adjMat[j][i] = 1;
|
||||
adjMat[i][j] = 1
|
||||
adjMat[j][i] = 1
|
||||
}
|
||||
|
||||
/* 删除边 */
|
||||
@@ -1113,15 +1113,15 @@ comments: true
|
||||
// 索引越界与相等处理
|
||||
if (i < 0 || j < 0 || i >= size() || j >= size() || i == j)
|
||||
throw IndexOutOfBoundsException()
|
||||
adjMat[i][j] = 0;
|
||||
adjMat[j][i] = 0;
|
||||
adjMat[i][j] = 0
|
||||
adjMat[j][i] = 0
|
||||
}
|
||||
|
||||
/* 打印邻接矩阵 */
|
||||
fun print() {
|
||||
print("顶点列表 = ")
|
||||
println(vertices);
|
||||
println("邻接矩阵 =");
|
||||
println(vertices)
|
||||
println("邻接矩阵 =")
|
||||
printMatrix(adjMat)
|
||||
}
|
||||
}
|
||||
@@ -2167,9 +2167,9 @@ comments: true
|
||||
init {
|
||||
// 添加所有顶点和边
|
||||
for (edge in edges) {
|
||||
addVertex(edge[0]!!);
|
||||
addVertex(edge[1]!!);
|
||||
addEdge(edge[0]!!, edge[1]!!);
|
||||
addVertex(edge[0]!!)
|
||||
addVertex(edge[1]!!)
|
||||
addEdge(edge[0]!!, edge[1]!!)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2184,7 +2184,7 @@ comments: true
|
||||
throw IllegalArgumentException()
|
||||
// 添加边 vet1 - vet2
|
||||
adjList[vet1]?.add(vet2)
|
||||
adjList[vet2]?.add(vet1);
|
||||
adjList[vet2]?.add(vet1)
|
||||
}
|
||||
|
||||
/* 删除边 */
|
||||
@@ -2192,8 +2192,8 @@ comments: true
|
||||
if (!adjList.containsKey(vet1) || !adjList.containsKey(vet2) || vet1 == vet2)
|
||||
throw IllegalArgumentException()
|
||||
// 删除边 vet1 - vet2
|
||||
adjList[vet1]?.remove(vet2);
|
||||
adjList[vet2]?.remove(vet1);
|
||||
adjList[vet1]?.remove(vet2)
|
||||
adjList[vet2]?.remove(vet1)
|
||||
}
|
||||
|
||||
/* 添加顶点 */
|
||||
@@ -2209,7 +2209,7 @@ comments: true
|
||||
if (!adjList.containsKey(vet))
|
||||
throw IllegalArgumentException()
|
||||
// 在邻接表中删除顶点 vet 对应的链表
|
||||
adjList.remove(vet);
|
||||
adjList.remove(vet)
|
||||
// 遍历其他顶点的链表,删除所有包含 vet 的边
|
||||
for (list in adjList.values) {
|
||||
list.remove(vet)
|
||||
|
||||
@@ -560,6 +560,7 @@ index = hash(key) % capacity
|
||||
/* 加法哈希 */
|
||||
fun addHash(key: String): Int {
|
||||
var hash = 0L
|
||||
val MODULUS = 1000000007
|
||||
for (c in key.toCharArray()) {
|
||||
hash = (hash + c.code) % MODULUS
|
||||
}
|
||||
@@ -569,6 +570,7 @@ index = hash(key) % capacity
|
||||
/* 乘法哈希 */
|
||||
fun mulHash(key: String): Int {
|
||||
var hash = 0L
|
||||
val MODULUS = 1000000007
|
||||
for (c in key.toCharArray()) {
|
||||
hash = (31 * hash + c.code) % MODULUS
|
||||
}
|
||||
@@ -578,6 +580,7 @@ index = hash(key) % capacity
|
||||
/* 异或哈希 */
|
||||
fun xorHash(key: String): Int {
|
||||
var hash = 0
|
||||
val MODULUS = 1000000007
|
||||
for (c in key.toCharArray()) {
|
||||
hash = hash xor c.code
|
||||
}
|
||||
@@ -587,6 +590,7 @@ index = hash(key) % capacity
|
||||
/* 旋转哈希 */
|
||||
fun rotHash(key: String): Int {
|
||||
var hash = 0L
|
||||
val MODULUS = 1000000007
|
||||
for (c in key.toCharArray()) {
|
||||
hash = ((hash shl 4) xor (hash shr 28) xor c.code.toLong()) % MODULUS
|
||||
}
|
||||
|
||||
@@ -1312,7 +1312,7 @@ comments: true
|
||||
|
||||
```kotlin title="hash_map_chaining.kt"
|
||||
/* 链式地址哈希表 */
|
||||
class HashMapChaining() {
|
||||
class HashMapChaining {
|
||||
var size: Int // 键值对数量
|
||||
var capacity: Int // 哈希表容量
|
||||
val loadThres: Double // 触发扩容的负载因子阈值
|
||||
|
||||
@@ -1646,7 +1646,8 @@ index = hash(key) % capacity
|
||||
fun valueSet(): MutableList<String> {
|
||||
val valueSet = mutableListOf<String>()
|
||||
for (pair in buckets) {
|
||||
pair?.let { valueSet.add(it._val) }
|
||||
if (pair != null)
|
||||
valueSet.add(pair._val)
|
||||
}
|
||||
return valueSet
|
||||
}
|
||||
@@ -1656,7 +1657,7 @@ index = hash(key) % capacity
|
||||
for (kv in pairSet()) {
|
||||
val key = kv.key
|
||||
val _val = kv._val
|
||||
println("${key} -> ${_val}")
|
||||
println("$key -> $_val")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -220,7 +220,9 @@ comments: true
|
||||
|
||||
/* 交换元素 */
|
||||
private fun swap(i: Int, j: Int) {
|
||||
maxHeap[i] = maxHeap[j].also { maxHeap[j] = maxHeap[i] }
|
||||
val temp = maxHeap[i]
|
||||
maxHeap[i] = maxHeap[j]
|
||||
maxHeap[j] = temp
|
||||
}
|
||||
|
||||
/* 获取堆大小 */
|
||||
|
||||
@@ -650,7 +650,7 @@ comments: true
|
||||
|
||||
/* 获取父节点的索引 */
|
||||
int parent(MaxHeap *maxHeap, int i) {
|
||||
return (i - 1) / 2;
|
||||
return (i - 1) / 2; // 向下取整
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
@@ -264,7 +264,9 @@ comments: true
|
||||
for (j in 0..<i) {
|
||||
if (nums[j] > nums[j + 1]) {
|
||||
// 交换 nums[j] 与 nums[j + 1]
|
||||
nums[j] = nums[j + 1].also { nums[j + 1] = nums[j] }
|
||||
val temp = nums[j]
|
||||
nums[j] = nums[j + 1]
|
||||
nums[j + 1] = temp
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -571,7 +573,9 @@ comments: true
|
||||
for (j in 0..<i) {
|
||||
if (nums[j] > nums[j + 1]) {
|
||||
// 交换 nums[j] 与 nums[j + 1]
|
||||
nums[j] = nums[j + 1].also { nums[j] = nums[j + 1] }
|
||||
val temp = nums[j]
|
||||
nums[j] = nums[j + 1]
|
||||
nums[j + 1] = temp
|
||||
flag = true // 记录交换元素
|
||||
}
|
||||
}
|
||||
|
||||
@@ -553,7 +553,9 @@ comments: true
|
||||
if (ma == i)
|
||||
break
|
||||
// 交换两节点
|
||||
nums[i] = nums[ma].also { nums[ma] = nums[i] }
|
||||
val temp = nums[i]
|
||||
nums[i] = nums[ma]
|
||||
nums[ma] = temp
|
||||
// 循环向下堆化
|
||||
i = ma
|
||||
}
|
||||
@@ -568,7 +570,9 @@ comments: true
|
||||
// 从堆中提取最大元素,循环 n-1 轮
|
||||
for (i in nums.size - 1 downTo 1) {
|
||||
// 交换根节点与最右叶节点(交换首元素与尾元素)
|
||||
nums[0] = nums[i].also { nums[i] = nums[0] }
|
||||
val temp = nums[0]
|
||||
nums[0] = nums[i]
|
||||
nums[i] = temp
|
||||
// 以根节点为起点,从顶至底进行堆化
|
||||
siftDown(nums, i, 0)
|
||||
}
|
||||
|
||||
@@ -328,7 +328,9 @@ comments: true
|
||||
```kotlin title="quick_sort.kt"
|
||||
/* 元素交换 */
|
||||
fun swap(nums: IntArray, i: Int, j: Int) {
|
||||
nums[i] = nums[j].also { nums[j] = nums[i] }
|
||||
val temp = nums[i]
|
||||
nums[i] = nums[j]
|
||||
nums[j] = temp
|
||||
}
|
||||
|
||||
/* 哨兵划分 */
|
||||
|
||||
@@ -296,7 +296,9 @@ comments: true
|
||||
k = j // 记录最小元素的索引
|
||||
}
|
||||
// 将该最小元素与未排序区间的首个元素交换
|
||||
nums[i] = nums[k].also { nums[k] = nums[i] }
|
||||
val temp = nums[i]
|
||||
nums[i] = nums[k]
|
||||
nums[k] = temp
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@@ -1092,7 +1092,7 @@ comments: true
|
||||
fun pop(): Int? {
|
||||
val num = peek()
|
||||
stackPeek = stackPeek?.next
|
||||
stkSize--;
|
||||
stkSize--
|
||||
return num
|
||||
}
|
||||
|
||||
|
||||
@@ -125,7 +125,7 @@ comments: true
|
||||
```kotlin title=""
|
||||
/* 二叉树的数组表示 */
|
||||
// 使用 null 来表示空位
|
||||
val tree = mutableListOf( 1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15 )
|
||||
val tree = arrayOf( 1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15 )
|
||||
```
|
||||
|
||||
=== "Ruby"
|
||||
@@ -1172,7 +1172,7 @@ comments: true
|
||||
=== "Kotlin"
|
||||
|
||||
```kotlin title="array_binary_tree.kt"
|
||||
/* 构造方法 */
|
||||
/* 数组表示下的二叉树类 */
|
||||
class ArrayBinaryTree(val tree: MutableList<Int?>) {
|
||||
/* 列表容量 */
|
||||
fun size(): Int {
|
||||
|
||||
@@ -305,7 +305,7 @@ comments: true
|
||||
val list = mutableListOf<Int>()
|
||||
while (queue.isNotEmpty()) {
|
||||
val node = queue.poll() // 队列出队
|
||||
list.add(node?._val!!) // 保存节点值
|
||||
list.add(node?._val!!) // 保存节点值
|
||||
if (node.left != null)
|
||||
queue.offer(node.left) // 左子节点入队
|
||||
if (node.right != null)
|
||||
|
||||
Reference in New Issue
Block a user