mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-10 14:20:43 +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]
|
||||
|
||||
Reference in New Issue
Block a user