mirror of
https://github.com/krahets/hello-algo.git
synced 2026-06-14 22:26:38 +08:00
build
This commit is contained in:
@@ -694,8 +694,8 @@ Please note that after deletion, the former last element becomes "meaningless,"
|
||||
### 删除索引 index 处的元素 ###
|
||||
def remove(nums, index)
|
||||
# 把索引 index 之后的所有元素向前移动一位
|
||||
for i in index...nums.length
|
||||
nums[i] = nums[i + 1] || 0
|
||||
for i in index...(nums.length - 1)
|
||||
nums[i] = nums[i + 1]
|
||||
end
|
||||
end
|
||||
```
|
||||
@@ -934,7 +934,7 @@ In most programming languages, we can traverse an array either by using indices
|
||||
count += nums[i]
|
||||
}
|
||||
// 直接遍历数组元素
|
||||
for (j: Int in nums) {
|
||||
for (j in nums) {
|
||||
count += j
|
||||
}
|
||||
}
|
||||
@@ -1140,7 +1140,8 @@ Because arrays are linear data structures, this operation is commonly referred t
|
||||
/* 在数组中查找指定元素 */
|
||||
fun find(nums: IntArray, target: Int): Int {
|
||||
for (i in nums.indices) {
|
||||
if (nums[i] == target) return i
|
||||
if (nums[i] == target)
|
||||
return i
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
@@ -544,7 +544,7 @@ By comparison, inserting an element into an array has a time complexity of $O(n)
|
||||
=== "Kotlin"
|
||||
|
||||
```kotlin title="linked_list.kt"
|
||||
/* 在链表的节点 n0 之后插入节点p */
|
||||
/* 在链表的节点 n0 之后插入节点 P */
|
||||
fun insert(n0: ListNode?, p: ListNode?) {
|
||||
val n1 = n0?.next
|
||||
p?.next = n1
|
||||
@@ -758,9 +758,11 @@ It's important to note that even though node `P` continues to point to `n1` afte
|
||||
```kotlin title="linked_list.kt"
|
||||
/* 删除链表的节点 n0 之后的首个节点 */
|
||||
fun remove(n0: ListNode?) {
|
||||
val p = n0?.next
|
||||
if (n0?.next == null)
|
||||
return
|
||||
val p = n0.next
|
||||
val n1 = p?.next
|
||||
n0?.next = n1
|
||||
n0.next = n1
|
||||
}
|
||||
```
|
||||
|
||||
@@ -965,7 +967,9 @@ It's important to note that even though node `P` continues to point to `n1` afte
|
||||
fun access(head: ListNode?, index: Int): ListNode? {
|
||||
var h = head
|
||||
for (i in 0..<index) {
|
||||
h = h?.next
|
||||
if (h == null)
|
||||
return null
|
||||
h = h.next
|
||||
}
|
||||
return h
|
||||
}
|
||||
@@ -1196,7 +1200,8 @@ 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) return index
|
||||
if (h.value == target)
|
||||
return index
|
||||
h = h.next
|
||||
index++
|
||||
}
|
||||
|
||||
@@ -2047,11 +2047,11 @@ To enhance our understanding of how lists work, we will attempt to implement a s
|
||||
/* 列表类 */
|
||||
class MyList {
|
||||
private var arr: IntArray = intArrayOf() // 数组(存储列表元素)
|
||||
private var capacity = 10 // 列表容量
|
||||
private var size = 0 // 列表长度(当前元素数量)
|
||||
private var extendRatio = 2 // 每次列表扩容的倍数
|
||||
private var capacity: Int = 10 // 列表容量
|
||||
private var size: Int = 0 // 列表长度(当前元素数量)
|
||||
private var extendRatio: Int = 2 // 每次列表扩容的倍数
|
||||
|
||||
/* 构造函数 */
|
||||
/* 构造方法 */
|
||||
init {
|
||||
arr = IntArray(capacity)
|
||||
}
|
||||
@@ -2070,7 +2070,7 @@ To enhance our understanding of how lists work, we will attempt to implement a s
|
||||
fun get(index: Int): Int {
|
||||
// 索引如果越界,则抛出异常,下同
|
||||
if (index < 0 || index >= size)
|
||||
throw IndexOutOfBoundsException()
|
||||
throw IndexOutOfBoundsException("索引越界")
|
||||
return arr[index]
|
||||
}
|
||||
|
||||
@@ -2110,7 +2110,7 @@ To enhance our understanding of how lists work, we will attempt to implement a s
|
||||
fun remove(index: Int): Int {
|
||||
if (index < 0 || index >= size)
|
||||
throw IndexOutOfBoundsException("索引越界")
|
||||
val num: Int = arr[index]
|
||||
val num = arr[index]
|
||||
// 将将索引 index 之后的元素都向前移动一位
|
||||
for (j in index..<size - 1)
|
||||
arr[j] = arr[j + 1]
|
||||
|
||||
@@ -1820,9 +1820,9 @@ Quadratic order is common in matrices and graphs, where the number of elements i
|
||||
/* 平方阶 */
|
||||
fun quadratic(n: Int) {
|
||||
// 矩阵占用 O(n^2) 空间
|
||||
val numMatrix: Array<Array<Int>?> = arrayOfNulls(n)
|
||||
val numMatrix = arrayOfNulls<Array<Int>?>(n)
|
||||
// 二维列表占用 O(n^2) 空间
|
||||
val numList: MutableList<MutableList<Int>> = arrayListOf()
|
||||
val numList = mutableListOf<MutableList<Int>>()
|
||||
for (i in 0..<n) {
|
||||
val tmp = mutableListOf<Int>()
|
||||
for (j in 0..<n) {
|
||||
|
||||
@@ -1133,7 +1133,7 @@ Constant order means the number of operations is independent of the input data s
|
||||
/* 常数阶 */
|
||||
fun constant(n: Int): Int {
|
||||
var count = 0
|
||||
val size = 10_0000
|
||||
val size = 100000
|
||||
for (i in 0..<size)
|
||||
count++
|
||||
return count
|
||||
@@ -2056,7 +2056,7 @@ For instance, in bubble sort, the outer loop runs $n - 1$ times, and the inner l
|
||||
```kotlin title="time_complexity.kt"
|
||||
/* 平方阶(冒泡排序) */
|
||||
fun bubbleSort(nums: IntArray): Int {
|
||||
var count = 0
|
||||
var count = 0 // 计数器
|
||||
// 外循环:未排序区间为 [0, i]
|
||||
for (i in nums.size - 1 downTo 1) {
|
||||
// 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
|
||||
@@ -3119,7 +3119,7 @@ Linear-logarithmic order often appears in nested loops, with the complexities of
|
||||
if (n <= 1)
|
||||
return 1
|
||||
var count = linearLogRecur(n / 2) + linearLogRecur(n / 2)
|
||||
for (i in 0..<n.toInt()) {
|
||||
for (i in 0..<n) {
|
||||
count++
|
||||
}
|
||||
return count
|
||||
@@ -3747,10 +3747,9 @@ The "worst-case time complexity" corresponds to the asymptotic upper bound, deno
|
||||
for (i in 0..<n) {
|
||||
nums[i] = i + 1
|
||||
}
|
||||
// 随机打乱数组元素
|
||||
val mutableList = nums.toMutableList()
|
||||
// 随机打乱数组元素
|
||||
mutableList.shuffle()
|
||||
// Integer[] -> int[]
|
||||
val res = arrayOfNulls<Int>(n)
|
||||
for (i in 0..<n) {
|
||||
res[i] = mutableList[i]
|
||||
|
||||
@@ -143,7 +143,7 @@ Now we can answer the initial question: **The representation of `float` includes
|
||||
|
||||
**However, the trade-off for `float`'s expanded range is a sacrifice in precision**. The integer type `int` uses all 32 bits to represent the number, with values evenly distributed; but due to the exponent bit, the larger the value of a `float`, the greater the difference between adjacent numbers.
|
||||
|
||||
As shown in the Table 3-2 , exponent bits $E = 0$ and $E = 255$ have special meanings, **used to represent zero, infinity, $\mathrm{NaN}$, etc.**
|
||||
As shown in the Table 3-2 , exponent bits $\mathrm{E} = 0$ and $\mathrm{E} = 255$ have special meanings, **used to represent zero, infinity, $\mathrm{NaN}$, etc.**
|
||||
|
||||
<p align="center"> Table 3-2 Meaning of exponent bits </p>
|
||||
|
||||
|
||||
@@ -2824,6 +2824,9 @@ The code below implements an open addressing (linear probing) hash table with la
|
||||
free(pair);
|
||||
}
|
||||
}
|
||||
free(hashMap->buckets);
|
||||
free(hashMap->TOMBSTONE);
|
||||
free(hashMap);
|
||||
}
|
||||
|
||||
/* 哈希函数 */
|
||||
|
||||
@@ -1797,8 +1797,8 @@ The implementation code is as follows:
|
||||
if (fNext) {
|
||||
fNext->prev = NULL;
|
||||
deque->front->next = NULL;
|
||||
delDoublyListNode(deque->front);
|
||||
}
|
||||
delDoublyListNode(deque->front);
|
||||
deque->front = fNext; // 更新头节点
|
||||
}
|
||||
// 队尾出队操作
|
||||
@@ -1808,8 +1808,8 @@ The implementation code is as follows:
|
||||
if (rPrev) {
|
||||
rPrev->next = NULL;
|
||||
deque->rear->prev = NULL;
|
||||
delDoublyListNode(deque->rear);
|
||||
}
|
||||
delDoublyListNode(deque->rear);
|
||||
deque->rear = rPrev; // 更新尾节点
|
||||
}
|
||||
deque->queSize--; // 更新队列长度
|
||||
|
||||
Reference in New Issue
Block a user