mirror of
				https://github.com/krahets/hello-algo.git
				synced 2025-11-04 06:07:20 +08:00 
			
		
		
		
	Improve the consistency between code and comments in Kotlin (#1268)
* style(kotlin): Make code and comments consistent. * style(kotlin): convert comment location. * style(c): Add missing comment. * style(kotlin): Remove redundant semicolon, parenthesis and brace * style(kotlin): Put constants inside the function. * style(kotlin): fix unnecessary indentation. * style(swift): Add missing comment. * style(kotlin): Add missing comment. * style(kotlin): Remove redundant comment. * style(kotlin): Add missing comment. * Update linked_list.kt * style(csharp,js,ts): Add missing comment. * style(kotlin): Remove empty lines. * Update list.cs * Update list.js * Update list.ts * roll back to commit 1 * style(cs,js,ts): Add missing comment in docfile. * style(kotlin): Use normal element swapping instead of scope functions.
This commit is contained in:
		@ -52,7 +52,7 @@ int right(MaxHeap *maxHeap, int i) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
/* 获取父节点的索引 */
 | 
					/* 获取父节点的索引 */
 | 
				
			||||||
int parent(MaxHeap *maxHeap, int i) {
 | 
					int parent(MaxHeap *maxHeap, int i) {
 | 
				
			||||||
    return (i - 1) / 2;
 | 
					    return (i - 1) / 2; // 向下取整
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* 交换元素 */
 | 
					/* 交换元素 */
 | 
				
			||||||
 | 
				
			|||||||
@ -20,6 +20,7 @@ fun insert(n0: ListNode?, p: ListNode?) {
 | 
				
			|||||||
fun remove(n0: ListNode?) {
 | 
					fun remove(n0: ListNode?) {
 | 
				
			||||||
    if (n0?.next == null)
 | 
					    if (n0?.next == null)
 | 
				
			||||||
        return
 | 
					        return
 | 
				
			||||||
 | 
					    // n0 -> P -> n1
 | 
				
			||||||
    val p = n0.next
 | 
					    val p = n0.next
 | 
				
			||||||
    val n1 = p?.next
 | 
					    val n1 = p?.next
 | 
				
			||||||
    n0.next = n1
 | 
					    n0.next = n1
 | 
				
			||||||
@ -78,10 +79,10 @@ fun main() {
 | 
				
			|||||||
    printLinkedList(n0)
 | 
					    printLinkedList(n0)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /* 访问节点 */
 | 
					    /* 访问节点 */
 | 
				
			||||||
    val node: ListNode = access(n0, 3)!!
 | 
					    val node = access(n0, 3)!!
 | 
				
			||||||
    println("链表中索引 3 处的节点的值 = ${node._val}")
 | 
					    println("链表中索引 3 处的节点的值 = ${node._val}")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /* 查找节点 */
 | 
					    /* 查找节点 */
 | 
				
			||||||
    val index: Int = find(n0, 2)
 | 
					    val index = find(n0, 2)
 | 
				
			||||||
    println("链表中值为 2 的节点的索引 = $index")
 | 
					    println("链表中值为 2 的节点的索引 = $index")
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@ -26,6 +26,7 @@ fun forLoopRecur(n: Int): Int {
 | 
				
			|||||||
    var res = 0
 | 
					    var res = 0
 | 
				
			||||||
    // 递: 递归调用
 | 
					    // 递: 递归调用
 | 
				
			||||||
    for (i in n downTo 0) {
 | 
					    for (i in n downTo 0) {
 | 
				
			||||||
 | 
					        // 通过“入栈操作”模拟“递”
 | 
				
			||||||
        stack.push(i)
 | 
					        stack.push(i)
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    // 归: 返回结果
 | 
					    // 归: 返回结果
 | 
				
			||||||
 | 
				
			|||||||
@ -18,7 +18,6 @@ fun constant(n: Int): Int {
 | 
				
			|||||||
/* 线性阶 */
 | 
					/* 线性阶 */
 | 
				
			||||||
fun linear(n: Int): Int {
 | 
					fun linear(n: Int): Int {
 | 
				
			||||||
    var count = 0
 | 
					    var count = 0
 | 
				
			||||||
    // 循环次数与数组长度成正比
 | 
					 | 
				
			||||||
    for (i in 0..<n)
 | 
					    for (i in 0..<n)
 | 
				
			||||||
        count++
 | 
					        count++
 | 
				
			||||||
    return count
 | 
					    return count
 | 
				
			||||||
@ -55,7 +54,9 @@ fun bubbleSort(nums: IntArray): Int {
 | 
				
			|||||||
        for (j in 0..<i) {
 | 
					        for (j in 0..<i) {
 | 
				
			||||||
            if (nums[j] > nums[j + 1]) {
 | 
					            if (nums[j] > nums[j + 1]) {
 | 
				
			||||||
                // 交换 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 个单元操作
 | 
					                count += 3 // 元素交换包含 3 个单元操作
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
@ -66,8 +67,8 @@ fun bubbleSort(nums: IntArray): Int {
 | 
				
			|||||||
/* 指数阶(循环实现) */
 | 
					/* 指数阶(循环实现) */
 | 
				
			||||||
fun exponential(n: Int): Int {
 | 
					fun exponential(n: Int): Int {
 | 
				
			||||||
    var count = 0
 | 
					    var count = 0
 | 
				
			||||||
    // 细胞每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)
 | 
					 | 
				
			||||||
    var base = 1
 | 
					    var base = 1
 | 
				
			||||||
 | 
					    // 细胞每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)
 | 
				
			||||||
    for (i in 0..<n) {
 | 
					    for (i in 0..<n) {
 | 
				
			||||||
        for (j in 0..<base) {
 | 
					        for (j in 0..<base) {
 | 
				
			||||||
            count++
 | 
					            count++
 | 
				
			||||||
 | 
				
			|||||||
@ -13,12 +13,11 @@ fun randomNumbers(n: Int): Array<Int?> {
 | 
				
			|||||||
    for (i in 0..<n) {
 | 
					    for (i in 0..<n) {
 | 
				
			||||||
        nums[i] = i + 1
 | 
					        nums[i] = i + 1
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    val mutableList = nums.toMutableList()
 | 
					 | 
				
			||||||
    // 随机打乱数组元素
 | 
					    // 随机打乱数组元素
 | 
				
			||||||
    mutableList.shuffle()
 | 
					    nums.shuffle()
 | 
				
			||||||
    val res = arrayOfNulls<Int>(n)
 | 
					    val res = arrayOfNulls<Int>(n)
 | 
				
			||||||
    for (i in 0..<n) {
 | 
					    for (i in 0..<n) {
 | 
				
			||||||
        res[i] = mutableList[i]
 | 
					        res[i] = nums[i]
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    return res
 | 
					    return res
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -27,7 +27,9 @@ fun climbingStairsDPComp(n: Int): Int {
 | 
				
			|||||||
    var a = 1
 | 
					    var a = 1
 | 
				
			||||||
    var b = 2
 | 
					    var b = 2
 | 
				
			||||||
    for (i in 3..n) {
 | 
					    for (i in 3..n) {
 | 
				
			||||||
        b += a.also { a = b }
 | 
					        val temp = b
 | 
				
			||||||
 | 
					        b += a
 | 
				
			||||||
 | 
					        a = temp
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    return b
 | 
					    return b
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -59,11 +59,7 @@ fun knapsackDFSMem(
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* 0-1 背包:动态规划 */
 | 
					/* 0-1 背包:动态规划 */
 | 
				
			||||||
fun knapsackDP(
 | 
					fun knapsackDP(wgt: IntArray, _val: IntArray, cap: Int): Int {
 | 
				
			||||||
    wgt: IntArray,
 | 
					 | 
				
			||||||
    _val: IntArray,
 | 
					 | 
				
			||||||
    cap: Int
 | 
					 | 
				
			||||||
): Int {
 | 
					 | 
				
			||||||
    val n = wgt.size
 | 
					    val n = wgt.size
 | 
				
			||||||
    // 初始化 dp 表
 | 
					    // 初始化 dp 表
 | 
				
			||||||
    val dp = Array(n + 1) { IntArray(cap + 1) }
 | 
					    val dp = Array(n + 1) { IntArray(cap + 1) }
 | 
				
			||||||
@ -83,11 +79,7 @@ fun knapsackDP(
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* 0-1 背包:空间优化后的动态规划 */
 | 
					/* 0-1 背包:空间优化后的动态规划 */
 | 
				
			||||||
fun knapsackDPComp(
 | 
					fun knapsackDPComp(wgt: IntArray, _val: IntArray, cap: Int): Int {
 | 
				
			||||||
    wgt: IntArray,
 | 
					 | 
				
			||||||
    _val: IntArray,
 | 
					 | 
				
			||||||
    cap: Int
 | 
					 | 
				
			||||||
): Int {
 | 
					 | 
				
			||||||
    val n = wgt.size
 | 
					    val n = wgt.size
 | 
				
			||||||
    // 初始化 dp 表
 | 
					    // 初始化 dp 表
 | 
				
			||||||
    val dp = IntArray(cap + 1)
 | 
					    val dp = IntArray(cap + 1)
 | 
				
			||||||
@ -97,8 +89,7 @@ fun knapsackDPComp(
 | 
				
			|||||||
        for (c in cap downTo 1) {
 | 
					        for (c in cap downTo 1) {
 | 
				
			||||||
            if (wgt[i - 1] <= c) {
 | 
					            if (wgt[i - 1] <= c) {
 | 
				
			||||||
                // 不选和选物品 i 这两种方案的较大值
 | 
					                // 不选和选物品 i 这两种方案的较大值
 | 
				
			||||||
                dp[c] =
 | 
					                dp[c] = max(dp[c], dp[c - wgt[i - 1]] + _val[i - 1])
 | 
				
			||||||
                    max(dp[c], dp[c - wgt[i - 1]] + _val[i - 1])
 | 
					 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
				
			|||||||
@ -17,9 +17,9 @@ class GraphAdjList(edges: Array<Array<Vertex?>>) {
 | 
				
			|||||||
    init {
 | 
					    init {
 | 
				
			||||||
        // 添加所有顶点和边
 | 
					        // 添加所有顶点和边
 | 
				
			||||||
        for (edge in edges) {
 | 
					        for (edge in edges) {
 | 
				
			||||||
            addVertex(edge[0]!!);
 | 
					            addVertex(edge[0]!!)
 | 
				
			||||||
            addVertex(edge[1]!!);
 | 
					            addVertex(edge[1]!!)
 | 
				
			||||||
            addEdge(edge[0]!!, edge[1]!!);
 | 
					            addEdge(edge[0]!!, edge[1]!!)
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -34,7 +34,7 @@ class GraphAdjList(edges: Array<Array<Vertex?>>) {
 | 
				
			|||||||
            throw IllegalArgumentException()
 | 
					            throw IllegalArgumentException()
 | 
				
			||||||
        // 添加边 vet1 - vet2
 | 
					        // 添加边 vet1 - vet2
 | 
				
			||||||
        adjList[vet1]?.add(vet2)
 | 
					        adjList[vet1]?.add(vet2)
 | 
				
			||||||
        adjList[vet2]?.add(vet1);
 | 
					        adjList[vet2]?.add(vet1)
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /* 删除边 */
 | 
					    /* 删除边 */
 | 
				
			||||||
@ -42,8 +42,8 @@ class GraphAdjList(edges: Array<Array<Vertex?>>) {
 | 
				
			|||||||
        if (!adjList.containsKey(vet1) || !adjList.containsKey(vet2) || vet1 == vet2)
 | 
					        if (!adjList.containsKey(vet1) || !adjList.containsKey(vet2) || vet1 == vet2)
 | 
				
			||||||
            throw IllegalArgumentException()
 | 
					            throw IllegalArgumentException()
 | 
				
			||||||
        // 删除边 vet1 - vet2
 | 
					        // 删除边 vet1 - vet2
 | 
				
			||||||
        adjList[vet1]?.remove(vet2);
 | 
					        adjList[vet1]?.remove(vet2)
 | 
				
			||||||
        adjList[vet2]?.remove(vet1);
 | 
					        adjList[vet2]?.remove(vet1)
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /* 添加顶点 */
 | 
					    /* 添加顶点 */
 | 
				
			||||||
@ -59,7 +59,7 @@ class GraphAdjList(edges: Array<Array<Vertex?>>) {
 | 
				
			|||||||
        if (!adjList.containsKey(vet))
 | 
					        if (!adjList.containsKey(vet))
 | 
				
			||||||
            throw IllegalArgumentException()
 | 
					            throw IllegalArgumentException()
 | 
				
			||||||
        // 在邻接表中删除顶点 vet 对应的链表
 | 
					        // 在邻接表中删除顶点 vet 对应的链表
 | 
				
			||||||
        adjList.remove(vet);
 | 
					        adjList.remove(vet)
 | 
				
			||||||
        // 遍历其他顶点的链表,删除所有包含 vet 的边
 | 
					        // 遍历其他顶点的链表,删除所有包含 vet 的边
 | 
				
			||||||
        for (list in adjList.values) {
 | 
					        for (list in adjList.values) {
 | 
				
			||||||
            list.remove(vet)
 | 
					            list.remove(vet)
 | 
				
			||||||
 | 
				
			|||||||
@ -69,8 +69,8 @@ class GraphAdjMat(vertices: IntArray, edges: Array<IntArray>) {
 | 
				
			|||||||
        if (i < 0 || j < 0 || i >= size() || j >= size() || i == j)
 | 
					        if (i < 0 || j < 0 || i >= size() || j >= size() || i == j)
 | 
				
			||||||
            throw IndexOutOfBoundsException()
 | 
					            throw IndexOutOfBoundsException()
 | 
				
			||||||
        // 在无向图中,邻接矩阵关于主对角线对称,即满足 (i, j) == (j, i)
 | 
					        // 在无向图中,邻接矩阵关于主对角线对称,即满足 (i, j) == (j, i)
 | 
				
			||||||
        adjMat[i][j] = 1;
 | 
					        adjMat[i][j] = 1
 | 
				
			||||||
        adjMat[j][i] = 1;
 | 
					        adjMat[j][i] = 1
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /* 删除边 */
 | 
					    /* 删除边 */
 | 
				
			||||||
@ -79,15 +79,15 @@ class GraphAdjMat(vertices: IntArray, edges: Array<IntArray>) {
 | 
				
			|||||||
        // 索引越界与相等处理
 | 
					        // 索引越界与相等处理
 | 
				
			||||||
        if (i < 0 || j < 0 || i >= size() || j >= size() || i == j)
 | 
					        if (i < 0 || j < 0 || i >= size() || j >= size() || i == j)
 | 
				
			||||||
            throw IndexOutOfBoundsException()
 | 
					            throw IndexOutOfBoundsException()
 | 
				
			||||||
        adjMat[i][j] = 0;
 | 
					        adjMat[i][j] = 0
 | 
				
			||||||
        adjMat[j][i] = 0;
 | 
					        adjMat[j][i] = 0
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /* 打印邻接矩阵 */
 | 
					    /* 打印邻接矩阵 */
 | 
				
			||||||
    fun print() {
 | 
					    fun print() {
 | 
				
			||||||
        print("顶点列表 = ")
 | 
					        print("顶点列表 = ")
 | 
				
			||||||
        println(vertices);
 | 
					        println(vertices)
 | 
				
			||||||
        println("邻接矩阵 =");
 | 
					        println("邻接矩阵 =")
 | 
				
			||||||
        printMatrix(adjMat)
 | 
					        printMatrix(adjMat)
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -68,7 +68,8 @@ class ArrayHashMap {
 | 
				
			|||||||
    fun valueSet(): MutableList<String> {
 | 
					    fun valueSet(): MutableList<String> {
 | 
				
			||||||
        val valueSet = mutableListOf<String>()
 | 
					        val valueSet = mutableListOf<String>()
 | 
				
			||||||
        for (pair in buckets) {
 | 
					        for (pair in buckets) {
 | 
				
			||||||
            pair?.let { valueSet.add(it._val) }
 | 
					            if (pair != null)
 | 
				
			||||||
 | 
					                valueSet.add(pair._val)
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        return valueSet
 | 
					        return valueSet
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@ -78,7 +79,7 @@ class ArrayHashMap {
 | 
				
			|||||||
        for (kv in pairSet()) {
 | 
					        for (kv in pairSet()) {
 | 
				
			||||||
            val key = kv.key
 | 
					            val key = kv.key
 | 
				
			||||||
            val _val = kv._val
 | 
					            val _val = kv._val
 | 
				
			||||||
            println("${key} -> ${_val}")
 | 
					            println("$key -> $_val")
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -28,7 +28,7 @@ fun main() {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    val arr = arrayOf<Any>(12836, "小哈")
 | 
					    val arr = arrayOf<Any>(12836, "小哈")
 | 
				
			||||||
    val hashTup = arr.contentHashCode()
 | 
					    val hashTup = arr.contentHashCode()
 | 
				
			||||||
    println("数组 ${arr.contentToString()} 的哈希值为 ${hashTup}")
 | 
					    println("数组 ${arr.contentToString()} 的哈希值为 $hashTup")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    val obj = ListNode(0)
 | 
					    val obj = ListNode(0)
 | 
				
			||||||
    val hashObj = obj.hashCode()
 | 
					    val hashObj = obj.hashCode()
 | 
				
			||||||
 | 
				
			|||||||
@ -7,7 +7,7 @@
 | 
				
			|||||||
package chapter_hashing
 | 
					package chapter_hashing
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* 链式地址哈希表 */
 | 
					/* 链式地址哈希表 */
 | 
				
			||||||
class HashMapChaining() {
 | 
					class HashMapChaining {
 | 
				
			||||||
    var size: Int // 键值对数量
 | 
					    var size: Int // 键值对数量
 | 
				
			||||||
    var capacity: Int // 哈希表容量
 | 
					    var capacity: Int // 哈希表容量
 | 
				
			||||||
    val loadThres: Double // 触发扩容的负载因子阈值
 | 
					    val loadThres: Double // 触发扩容的负载因子阈值
 | 
				
			||||||
 | 
				
			|||||||
@ -6,11 +6,10 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
package chapter_hashing
 | 
					package chapter_hashing
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const val MODULUS = 1000000007
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
/* 加法哈希 */
 | 
					/* 加法哈希 */
 | 
				
			||||||
fun addHash(key: String): Int {
 | 
					fun addHash(key: String): Int {
 | 
				
			||||||
    var hash = 0L
 | 
					    var hash = 0L
 | 
				
			||||||
 | 
					    val MODULUS = 1000000007
 | 
				
			||||||
    for (c in key.toCharArray()) {
 | 
					    for (c in key.toCharArray()) {
 | 
				
			||||||
        hash = (hash + c.code) % MODULUS
 | 
					        hash = (hash + c.code) % MODULUS
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@ -20,6 +19,7 @@ fun addHash(key: String): Int {
 | 
				
			|||||||
/* 乘法哈希 */
 | 
					/* 乘法哈希 */
 | 
				
			||||||
fun mulHash(key: String): Int {
 | 
					fun mulHash(key: String): Int {
 | 
				
			||||||
    var hash = 0L
 | 
					    var hash = 0L
 | 
				
			||||||
 | 
					    val MODULUS = 1000000007
 | 
				
			||||||
    for (c in key.toCharArray()) {
 | 
					    for (c in key.toCharArray()) {
 | 
				
			||||||
        hash = (31 * hash + c.code) % MODULUS
 | 
					        hash = (31 * hash + c.code) % MODULUS
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@ -29,6 +29,7 @@ fun mulHash(key: String): Int {
 | 
				
			|||||||
/* 异或哈希 */
 | 
					/* 异或哈希 */
 | 
				
			||||||
fun xorHash(key: String): Int {
 | 
					fun xorHash(key: String): Int {
 | 
				
			||||||
    var hash = 0
 | 
					    var hash = 0
 | 
				
			||||||
 | 
					    val MODULUS = 1000000007
 | 
				
			||||||
    for (c in key.toCharArray()) {
 | 
					    for (c in key.toCharArray()) {
 | 
				
			||||||
        hash = hash xor c.code
 | 
					        hash = hash xor c.code
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@ -38,6 +39,7 @@ fun xorHash(key: String): Int {
 | 
				
			|||||||
/* 旋转哈希 */
 | 
					/* 旋转哈希 */
 | 
				
			||||||
fun rotHash(key: String): Int {
 | 
					fun rotHash(key: String): Int {
 | 
				
			||||||
    var hash = 0L
 | 
					    var hash = 0L
 | 
				
			||||||
 | 
					    val MODULUS = 1000000007
 | 
				
			||||||
    for (c in key.toCharArray()) {
 | 
					    for (c in key.toCharArray()) {
 | 
				
			||||||
        hash = ((hash shl 4) xor (hash shr 28) xor c.code.toLong()) % MODULUS
 | 
					        hash = ((hash shl 4) xor (hash shr 28) xor c.code.toLong()) % MODULUS
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
				
			|||||||
@ -41,7 +41,9 @@ class MaxHeap(nums: MutableList<Int>?) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    /* 交换元素 */
 | 
					    /* 交换元素 */
 | 
				
			||||||
    private fun swap(i: Int, j: Int) {
 | 
					    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
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /* 获取堆大小 */
 | 
					    /* 获取堆大小 */
 | 
				
			||||||
 | 
				
			|||||||
@ -14,7 +14,9 @@ fun bubbleSort(nums: IntArray) {
 | 
				
			|||||||
        for (j in 0..<i) {
 | 
					        for (j in 0..<i) {
 | 
				
			||||||
            if (nums[j] > nums[j + 1]) {
 | 
					            if (nums[j] > nums[j + 1]) {
 | 
				
			||||||
                // 交换 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
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@ -29,7 +31,9 @@ fun bubbleSortWithFlag(nums: IntArray) {
 | 
				
			|||||||
        for (j in 0..<i) {
 | 
					        for (j in 0..<i) {
 | 
				
			||||||
            if (nums[j] > nums[j + 1]) {
 | 
					            if (nums[j] > nums[j + 1]) {
 | 
				
			||||||
                // 交换 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 // 记录交换元素
 | 
					                flag = true // 记录交换元素
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
				
			|||||||
@ -22,7 +22,9 @@ fun siftDown(nums: IntArray, n: Int, li: Int) {
 | 
				
			|||||||
        if (ma == i) 
 | 
					        if (ma == i) 
 | 
				
			||||||
            break
 | 
					            break
 | 
				
			||||||
        // 交换两节点
 | 
					        // 交换两节点
 | 
				
			||||||
        nums[i] = nums[ma].also { nums[ma] = nums[i] }
 | 
					        val temp = nums[i]
 | 
				
			||||||
 | 
					        nums[i] = nums[ma]
 | 
				
			||||||
 | 
					        nums[ma] = temp
 | 
				
			||||||
        // 循环向下堆化
 | 
					        // 循环向下堆化
 | 
				
			||||||
        i = ma
 | 
					        i = ma
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@ -37,7 +39,9 @@ fun heapSort(nums: IntArray) {
 | 
				
			|||||||
    // 从堆中提取最大元素,循环 n-1 轮
 | 
					    // 从堆中提取最大元素,循环 n-1 轮
 | 
				
			||||||
    for (i in nums.size - 1 downTo 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)
 | 
					        siftDown(nums, i, 0)
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
				
			|||||||
@ -8,7 +8,9 @@ package chapter_sorting
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
/* 元素交换 */
 | 
					/* 元素交换 */
 | 
				
			||||||
fun swap(nums: IntArray, i: Int, j: Int) {
 | 
					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
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* 哨兵划分 */
 | 
					/* 哨兵划分 */
 | 
				
			||||||
 | 
				
			|||||||
@ -18,7 +18,9 @@ fun selectionSort(nums: IntArray) {
 | 
				
			|||||||
                k = j // 记录最小元素的索引
 | 
					                k = j // 记录最小元素的索引
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        // 将该最小元素与未排序区间的首个元素交换
 | 
					        // 将该最小元素与未排序区间的首个元素交换
 | 
				
			||||||
        nums[i] = nums[k].also { nums[k] = nums[i] }
 | 
					        val temp = nums[i]
 | 
				
			||||||
 | 
					        nums[i] = nums[k]
 | 
				
			||||||
 | 
					        nums[k] = temp
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -34,7 +34,7 @@ class LinkedListStack(
 | 
				
			|||||||
    fun pop(): Int? {
 | 
					    fun pop(): Int? {
 | 
				
			||||||
        val num = peek()
 | 
					        val num = peek()
 | 
				
			||||||
        stackPeek = stackPeek?.next
 | 
					        stackPeek = stackPeek?.next
 | 
				
			||||||
        stkSize--;
 | 
					        stkSize--
 | 
				
			||||||
        return num
 | 
					        return num
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -10,7 +10,6 @@ import utils.TreeNode
 | 
				
			|||||||
import utils.printTree
 | 
					import utils.printTree
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* 数组表示下的二叉树类 */
 | 
					/* 数组表示下的二叉树类 */
 | 
				
			||||||
/* 构造方法 */
 | 
					 | 
				
			||||||
class ArrayBinaryTree(private val tree: MutableList<Int?>) {
 | 
					class ArrayBinaryTree(private val tree: MutableList<Int?>) {
 | 
				
			||||||
    /* 列表容量 */
 | 
					    /* 列表容量 */
 | 
				
			||||||
    fun size(): Int {
 | 
					    fun size(): Int {
 | 
				
			||||||
@ -93,7 +92,7 @@ class ArrayBinaryTree(private val tree: MutableList<Int?>) {
 | 
				
			|||||||
/* Driver Code */
 | 
					/* Driver Code */
 | 
				
			||||||
fun main() {
 | 
					fun main() {
 | 
				
			||||||
    // 初始化二叉树
 | 
					    // 初始化二叉树
 | 
				
			||||||
    // 这里借助了一个从数组直接生成二叉树的函数
 | 
					    // 这里借助了一个从列表直接生成二叉树的函数
 | 
				
			||||||
    val arr = mutableListOf(1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15)
 | 
					    val arr = mutableListOf(1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    val root = TreeNode.listToTree(arr)
 | 
					    val root = TreeNode.listToTree(arr)
 | 
				
			||||||
 | 
				
			|||||||
@ -19,7 +19,7 @@ fun levelOrder(root: TreeNode?): MutableList<Int> {
 | 
				
			|||||||
    val list = mutableListOf<Int>()
 | 
					    val list = mutableListOf<Int>()
 | 
				
			||||||
    while (queue.isNotEmpty()) {
 | 
					    while (queue.isNotEmpty()) {
 | 
				
			||||||
        val node = queue.poll()      // 队列出队
 | 
					        val node = queue.poll()      // 队列出队
 | 
				
			||||||
        list.add(node?._val!!)      // 保存节点值
 | 
					        list.add(node?._val!!)       // 保存节点值
 | 
				
			||||||
        if (node.left != null)
 | 
					        if (node.left != null)
 | 
				
			||||||
            queue.offer(node.left)   // 左子节点入队
 | 
					            queue.offer(node.left)   // 左子节点入队
 | 
				
			||||||
        if (node.right != null)
 | 
					        if (node.right != null)
 | 
				
			||||||
@ -31,7 +31,7 @@ fun levelOrder(root: TreeNode?): MutableList<Int> {
 | 
				
			|||||||
/* Driver Code */
 | 
					/* Driver Code */
 | 
				
			||||||
fun main() {
 | 
					fun main() {
 | 
				
			||||||
    /* 初始化二叉树 */
 | 
					    /* 初始化二叉树 */
 | 
				
			||||||
    // 这里借助了一个从数组直接生成二叉树的函数
 | 
					    // 这里借助了一个从列表直接生成二叉树的函数
 | 
				
			||||||
    val root = TreeNode.listToTree(mutableListOf(1, 2, 3, 4, 5, 6, 7))
 | 
					    val root = TreeNode.listToTree(mutableListOf(1, 2, 3, 4, 5, 6, 7))
 | 
				
			||||||
    println("\n初始化二叉树\n")
 | 
					    println("\n初始化二叉树\n")
 | 
				
			||||||
    printTree(root)
 | 
					    printTree(root)
 | 
				
			||||||
 | 
				
			|||||||
@ -42,7 +42,7 @@ fun postOrder(root: TreeNode?) {
 | 
				
			|||||||
/* Driver Code */
 | 
					/* Driver Code */
 | 
				
			||||||
fun main() {
 | 
					fun main() {
 | 
				
			||||||
    /* 初始化二叉树 */
 | 
					    /* 初始化二叉树 */
 | 
				
			||||||
    // 这里借助了一个从数组直接生成二叉树的函数
 | 
					    // 这里借助了一个从列表直接生成二叉树的函数
 | 
				
			||||||
    val root = TreeNode.listToTree(mutableListOf(1, 2, 3, 4, 5, 6, 7))
 | 
					    val root = TreeNode.listToTree(mutableListOf(1, 2, 3, 4, 5, 6, 7))
 | 
				
			||||||
    println("\n初始化二叉树\n")
 | 
					    println("\n初始化二叉树\n")
 | 
				
			||||||
    printTree(root)
 | 
					    printTree(root)
 | 
				
			||||||
 | 
				
			|||||||
@ -16,7 +16,9 @@ func backtrack(choices: [Int], state: Int, n: Int, res: inout [Int]) {
 | 
				
			|||||||
        if state + choice > n {
 | 
					        if state + choice > n {
 | 
				
			||||||
            continue
 | 
					            continue
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					        // 尝试:做出选择,更新状态
 | 
				
			||||||
        backtrack(choices: choices, state: state + choice, n: n, res: &res)
 | 
					        backtrack(choices: choices, state: state + choice, n: n, res: &res)
 | 
				
			||||||
 | 
					        // 回退
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -379,10 +379,10 @@
 | 
				
			|||||||
    nums.Add(4);
 | 
					    nums.Add(4);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /* 在中间插入元素 */
 | 
					    /* 在中间插入元素 */
 | 
				
			||||||
    nums.Insert(3, 6);
 | 
					    nums.Insert(3, 6);  // 在索引 3 处插入数字 6
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /* 删除元素 */
 | 
					    /* 删除元素 */
 | 
				
			||||||
    nums.RemoveAt(3);
 | 
					    nums.RemoveAt(3);  // 删除索引 3 处的元素
 | 
				
			||||||
    ```
 | 
					    ```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
=== "Go"
 | 
					=== "Go"
 | 
				
			||||||
@ -439,10 +439,10 @@
 | 
				
			|||||||
    nums.push(4);
 | 
					    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"
 | 
					=== "TS"
 | 
				
			||||||
@ -459,10 +459,10 @@
 | 
				
			|||||||
    nums.push(4);
 | 
					    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"
 | 
					=== "Dart"
 | 
				
			||||||
 | 
				
			|||||||
@ -117,7 +117,7 @@
 | 
				
			|||||||
    ```kotlin title=""
 | 
					    ```kotlin title=""
 | 
				
			||||||
    /* 二叉树的数组表示 */
 | 
					    /* 二叉树的数组表示 */
 | 
				
			||||||
    // 使用 null 来表示空位
 | 
					    // 使用 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"
 | 
					=== "Ruby"
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user