mirror of
https://github.com/krahets/hello-algo.git
synced 2025-07-10 08:50:20 +08:00
Prepare 1.1.0 release (#1274)
* Update bucket_sort.c * Fix the comments in quick_sort.c * Update the announce badge * Sync zh and zh-hant versions * Update contributors list. * Sync zh and zh-hant versions. * Sync zh and zh-hant versions. * Update the contributors list * Update the version number
This commit is contained in:
@ -20,6 +20,7 @@ fun insert(n0: ListNode?, p: ListNode?) {
|
||||
fun remove(n0: ListNode?) {
|
||||
if (n0?.next == null)
|
||||
return
|
||||
// n0 -> P -> n1
|
||||
val p = n0.next
|
||||
val n1 = p?.next
|
||||
n0.next = n1
|
||||
@ -78,10 +79,10 @@ fun main() {
|
||||
printLinkedList(n0)
|
||||
|
||||
/* 訪問節點 */
|
||||
val node: ListNode = access(n0, 3)!!
|
||||
val node = access(n0, 3)!!
|
||||
println("鏈結串列中索引 3 處的節點的值 = ${node._val}")
|
||||
|
||||
/* 查詢節點 */
|
||||
val index: Int = find(n0, 2)
|
||||
val index = find(n0, 2)
|
||||
println("鏈結串列中值為 2 的節點的索引 = $index")
|
||||
}
|
@ -26,6 +26,7 @@ fun forLoopRecur(n: Int): Int {
|
||||
var res = 0
|
||||
// 遞: 遞迴呼叫
|
||||
for (i in n downTo 0) {
|
||||
// 透過“入堆疊操作”模擬“遞”
|
||||
stack.push(i)
|
||||
}
|
||||
// 迴: 返回結果
|
||||
|
@ -18,7 +18,6 @@ fun constant(n: Int): Int {
|
||||
/* 線性階 */
|
||||
fun linear(n: Int): Int {
|
||||
var count = 0
|
||||
// 迴圈次數與陣列長度成正比
|
||||
for (i in 0..<n)
|
||||
count++
|
||||
return count
|
||||
@ -55,7 +54,9 @@ fun bubbleSort(nums: IntArray): Int {
|
||||
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 個單元操作
|
||||
}
|
||||
}
|
||||
@ -66,8 +67,8 @@ fun bubbleSort(nums: IntArray): Int {
|
||||
/* 指數階(迴圈實現) */
|
||||
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++
|
||||
|
@ -13,12 +13,11 @@ fun randomNumbers(n: Int): Array<Int?> {
|
||||
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
|
||||
}
|
||||
|
@ -27,7 +27,9 @@ fun climbingStairsDPComp(n: Int): Int {
|
||||
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
|
||||
}
|
||||
|
@ -59,11 +59,7 @@ fun knapsackDFSMem(
|
||||
}
|
||||
|
||||
/* 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) }
|
||||
@ -83,11 +79,7 @@ fun knapsackDP(
|
||||
}
|
||||
|
||||
/* 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)
|
||||
@ -97,8 +89,7 @@ fun knapsackDPComp(
|
||||
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])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -17,9 +17,9 @@ class GraphAdjList(edges: Array<Array<Vertex?>>) {
|
||||
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]!!)
|
||||
}
|
||||
}
|
||||
|
||||
@ -34,7 +34,7 @@ class GraphAdjList(edges: Array<Array<Vertex?>>) {
|
||||
throw IllegalArgumentException()
|
||||
// 新增邊 vet1 - 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)
|
||||
throw IllegalArgumentException()
|
||||
// 刪除邊 vet1 - vet2
|
||||
adjList[vet1]?.remove(vet2);
|
||||
adjList[vet2]?.remove(vet1);
|
||||
adjList[vet1]?.remove(vet2)
|
||||
adjList[vet2]?.remove(vet1)
|
||||
}
|
||||
|
||||
/* 新增頂點 */
|
||||
@ -59,7 +59,7 @@ class GraphAdjList(edges: Array<Array<Vertex?>>) {
|
||||
if (!adjList.containsKey(vet))
|
||||
throw IllegalArgumentException()
|
||||
// 在鄰接表中刪除頂點 vet 對應的鏈結串列
|
||||
adjList.remove(vet);
|
||||
adjList.remove(vet)
|
||||
// 走訪其他頂點的鏈結串列,刪除所有包含 vet 的邊
|
||||
for (list in adjList.values) {
|
||||
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)
|
||||
throw IndexOutOfBoundsException()
|
||||
// 在無向圖中,鄰接矩陣關於主對角線對稱,即滿足 (i, j) == (j, i)
|
||||
adjMat[i][j] = 1;
|
||||
adjMat[j][i] = 1;
|
||||
adjMat[i][j] = 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)
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
@ -68,7 +68,8 @@ class ArrayHashMap {
|
||||
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
|
||||
}
|
||||
@ -78,7 +79,7 @@ class ArrayHashMap {
|
||||
for (kv in pairSet()) {
|
||||
val key = kv.key
|
||||
val _val = kv._val
|
||||
println("${key} -> ${_val}")
|
||||
println("$key -> $_val")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -28,7 +28,7 @@ fun main() {
|
||||
|
||||
val arr = arrayOf<Any>(12836, "小哈")
|
||||
val hashTup = arr.contentHashCode()
|
||||
println("陣列 ${arr.contentToString()} 的雜湊值為 ${hashTup}")
|
||||
println("陣列 ${arr.contentToString()} 的雜湊值為 $hashTup")
|
||||
|
||||
val obj = ListNode(0)
|
||||
val hashObj = obj.hashCode()
|
||||
|
@ -7,7 +7,7 @@
|
||||
package chapter_hashing
|
||||
|
||||
/* 鏈式位址雜湊表 */
|
||||
class HashMapChaining() {
|
||||
class HashMapChaining {
|
||||
var size: Int // 鍵值對數量
|
||||
var capacity: Int // 雜湊表容量
|
||||
val loadThres: Double // 觸發擴容的負載因子閾值
|
||||
|
@ -6,11 +6,10 @@
|
||||
|
||||
package chapter_hashing
|
||||
|
||||
const val MODULUS = 1000000007
|
||||
|
||||
/* 加法雜湊 */
|
||||
fun addHash(key: String): Int {
|
||||
var hash = 0L
|
||||
val MODULUS = 1000000007
|
||||
for (c in key.toCharArray()) {
|
||||
hash = (hash + c.code) % MODULUS
|
||||
}
|
||||
@ -20,6 +19,7 @@ fun addHash(key: String): Int {
|
||||
/* 乘法雜湊 */
|
||||
fun mulHash(key: String): Int {
|
||||
var hash = 0L
|
||||
val MODULUS = 1000000007
|
||||
for (c in key.toCharArray()) {
|
||||
hash = (31 * hash + c.code) % MODULUS
|
||||
}
|
||||
@ -29,6 +29,7 @@ fun mulHash(key: String): Int {
|
||||
/* 互斥或雜湊 */
|
||||
fun xorHash(key: String): Int {
|
||||
var hash = 0
|
||||
val MODULUS = 1000000007
|
||||
for (c in key.toCharArray()) {
|
||||
hash = hash xor c.code
|
||||
}
|
||||
@ -38,6 +39,7 @@ fun xorHash(key: String): Int {
|
||||
/* 旋轉雜湊 */
|
||||
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
|
||||
}
|
||||
|
@ -41,7 +41,9 @@ class MaxHeap(nums: MutableList<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) {
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -29,7 +31,9 @@ fun bubbleSortWithFlag(nums: IntArray) {
|
||||
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 // 記錄交換元素
|
||||
}
|
||||
}
|
||||
|
@ -22,7 +22,9 @@ fun siftDown(nums: IntArray, n: Int, li: Int) {
|
||||
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
|
||||
}
|
||||
@ -37,7 +39,9 @@ fun heapSort(nums: IntArray) {
|
||||
// 從堆積中提取最大元素,迴圈 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)
|
||||
}
|
||||
|
@ -8,7 +8,9 @@ package chapter_sorting
|
||||
|
||||
/* 元素交換 */
|
||||
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 // 記錄最小元素的索引
|
||||
}
|
||||
// 將該最小元素與未排序區間的首個元素交換
|
||||
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? {
|
||||
val num = peek()
|
||||
stackPeek = stackPeek?.next
|
||||
stkSize--;
|
||||
stkSize--
|
||||
return num
|
||||
}
|
||||
|
||||
|
@ -10,7 +10,6 @@ import utils.TreeNode
|
||||
import utils.printTree
|
||||
|
||||
/* 陣列表示下的二元樹類別 */
|
||||
/* 建構子 */
|
||||
class ArrayBinaryTree(private val tree: MutableList<Int?>) {
|
||||
/* 串列容量 */
|
||||
fun size(): Int {
|
||||
@ -93,7 +92,7 @@ class ArrayBinaryTree(private val tree: MutableList<Int?>) {
|
||||
/* Driver Code */
|
||||
fun main() {
|
||||
// 初始化二元樹
|
||||
// 這裡藉助了一個從陣列直接生成二元樹的函式
|
||||
// 這裡藉助了一個從串列直接生成二元樹的函式
|
||||
val arr = mutableListOf(1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15)
|
||||
|
||||
val root = TreeNode.listToTree(arr)
|
||||
|
@ -19,7 +19,7 @@ fun levelOrder(root: TreeNode?): MutableList<Int> {
|
||||
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)
|
||||
@ -31,7 +31,7 @@ fun levelOrder(root: TreeNode?): MutableList<Int> {
|
||||
/* Driver Code */
|
||||
fun main() {
|
||||
/* 初始化二元樹 */
|
||||
// 這裡藉助了一個從陣列直接生成二元樹的函式
|
||||
// 這裡藉助了一個從串列直接生成二元樹的函式
|
||||
val root = TreeNode.listToTree(mutableListOf(1, 2, 3, 4, 5, 6, 7))
|
||||
println("\n初始化二元樹\n")
|
||||
printTree(root)
|
||||
|
@ -42,7 +42,7 @@ fun postOrder(root: TreeNode?) {
|
||||
/* Driver Code */
|
||||
fun main() {
|
||||
/* 初始化二元樹 */
|
||||
// 這裡藉助了一個從陣列直接生成二元樹的函式
|
||||
// 這裡藉助了一個從串列直接生成二元樹的函式
|
||||
val root = TreeNode.listToTree(mutableListOf(1, 2, 3, 4, 5, 6, 7))
|
||||
println("\n初始化二元樹\n")
|
||||
printTree(root)
|
||||
|
Reference in New Issue
Block a user