-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Kotlin): Add Kotlin code for the array and linked list chapter (#…
…1070) * feat(kotlin):new kotlin support files * fix(kotlin): reviewed the formatting, comments and so on. * fix(kotlin): fix the indentation and format
- Loading branch information
Showing
4 changed files
with
383 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/** | ||
* File: array.kt | ||
* Created Time: 2024-01-25 | ||
* Author: curtishd (1023632660@qq.com) | ||
*/ | ||
|
||
package chapter_array_and_linkedlist | ||
|
||
import java.util.concurrent.ThreadLocalRandom | ||
|
||
/* 随机访问元素 */ | ||
fun randomAccess(nums: IntArray): Int { | ||
// 在区间 [0, nums.size) 中随机抽取一个数字 | ||
val randomIndex = ThreadLocalRandom.current().nextInt(0, nums.size) | ||
// 获取并返回随机元素 | ||
val randomNum = nums[randomIndex] | ||
return randomNum | ||
} | ||
|
||
/* 扩展数组长度 */ | ||
fun extend(nums: IntArray, enlarge: Int): IntArray { | ||
// 初始化一个扩展长度后的数组 | ||
val res = IntArray(nums.size + enlarge) | ||
// 将原数组中的所有元素复制到新数组 | ||
for (i in nums.indices) { | ||
res[i] = nums[i] | ||
} | ||
// 返回扩展后的新数组 | ||
return res | ||
} | ||
|
||
/* 在数组的索引 index 处插入元素 num */ | ||
fun insert(nums: IntArray, num: Int, index: Int) { | ||
// 把索引 index 以及之后的所有元素向后移动一位 | ||
for (i in nums.size - 1 downTo index + 1) { | ||
nums[i] = nums[i - 1] | ||
} | ||
// 将 num 赋给 index 处的元素 | ||
nums[index] = num | ||
} | ||
|
||
/* 删除索引 index 处的元素 */ | ||
fun remove(nums: IntArray, index: Int) { | ||
// 把索引 index 之后的所有元素向前移动一位 | ||
for (i in index..<nums.size - 1) { | ||
nums[i] = nums[i + 1] | ||
} | ||
} | ||
|
||
/* 遍历数组 */ | ||
fun traverse(nums: IntArray) { | ||
var count = 0 | ||
// 通过索引遍历数组 | ||
for (i in nums.indices) { | ||
count += nums[i] | ||
} | ||
// 直接遍历数组元素 | ||
for (j: Int in nums) { | ||
count += j | ||
} | ||
} | ||
|
||
/* 在数组中查找指定元素 */ | ||
fun find(nums: IntArray, target: Int): Int { | ||
for (i in nums.indices) { | ||
if (nums[i] == target) return i | ||
} | ||
return -1 | ||
} | ||
|
||
/* Driver Code */ | ||
fun main() { | ||
/* 初始化数组 */ | ||
val arr = IntArray(5) | ||
println("数组 arr = ${arr.contentToString()}") | ||
var nums = intArrayOf(1, 3, 2, 5, 4) | ||
println("数组 nums = ${nums.contentToString()}") | ||
|
||
/* 随机访问 */ | ||
val randomNum: Int = randomAccess(nums) | ||
println("在 nums 中获取随机元素 $randomNum") | ||
|
||
/* 长度扩展 */ | ||
nums = extend(nums, 3) | ||
println("将数组长度扩展至 8 ,得到 nums = ${nums.contentToString()}") | ||
|
||
/* 插入元素 */ | ||
insert(nums, 6, 3) | ||
println("在索引 3 处插入数字 6 ,得到 nums = ${nums.contentToString()}") | ||
|
||
/* 删除元素 */ | ||
remove(nums, 2) | ||
println("删除索引 2 处的元素,得到 nums = ${nums.contentToString()}") | ||
|
||
/* 遍历数组 */ | ||
traverse(nums) | ||
|
||
/* 查找元素 */ | ||
val index: Int = find(nums, 3) | ||
println("在 nums 中查找元素 3 ,得到索引 = $index") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/** | ||
* File: linked_list.kt | ||
* Created Time: 2024-01-25 | ||
* Author: curtishd (1023632660@qq.com) | ||
*/ | ||
|
||
package chapter_array_and_linkedlist | ||
|
||
import utils.ListNode | ||
import utils.printLinkedList | ||
|
||
/* 在链表的节点 n0 之后插入节点p */ | ||
fun insert(n0: ListNode?, p: ListNode?) { | ||
val n1 = n0?.next | ||
p?.next = n1 | ||
n0?.next = p | ||
} | ||
|
||
/* 删除链表的节点 n0 之后的首个节点 */ | ||
fun remove(n0: ListNode?) { | ||
val p = n0?.next | ||
val n1 = p?.next | ||
n0?.next = n1 | ||
} | ||
|
||
/* 访问链表中索引为 index 的节点 */ | ||
fun access(head: ListNode?, index: Int): ListNode? { | ||
var h = head | ||
for (i in 0..<index) { | ||
h = h?.next | ||
} | ||
return h | ||
} | ||
|
||
/* 在链表中查找值为 target 的首个节点 */ | ||
fun find(head: ListNode?, target: Int): Int { | ||
var index = 0 | ||
var h = head | ||
while (h != null) { | ||
if (h.value == target) return index | ||
h = h.next | ||
index++ | ||
} | ||
return -1 | ||
} | ||
|
||
/* Driver Code */ | ||
fun main() { | ||
// 初始化各个节点 | ||
val n0 = ListNode(1) | ||
val n1 = ListNode(3) | ||
val n2 = ListNode(2) | ||
val n3 = ListNode(5) | ||
val n4 = ListNode(4) | ||
|
||
// 构建节点之间的引用 | ||
n0.next = n1 | ||
n1.next = n2 | ||
n2.next = n3 | ||
n3.next = n4 | ||
println("初始化的链表为") | ||
printLinkedList(n0) | ||
|
||
insert(n0, ListNode(0)) | ||
println("插入节点后的链表为") | ||
printLinkedList(n0) | ||
|
||
/* 删除节点 */ | ||
remove(n0) | ||
println("删除节点后的链表为") | ||
printLinkedList(n0) | ||
|
||
/* 访问节点 */ | ||
val node: ListNode = access(n0, 3)!! | ||
println("链表中索引 3 处的节点的值 = ${node.value}") | ||
|
||
/* 查找节点 */ | ||
val index: Int = find(n0, 2) | ||
println("链表中值为 2 的节点的索引 = $index") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/** | ||
* File: list.kt | ||
* Created Time: 2024-01-25 | ||
* Author: curtishd (1023632660@qq.com) | ||
*/ | ||
|
||
package chapter_array_and_linkedlist | ||
|
||
/* Driver Code */ | ||
fun main() { | ||
// 可变集合 | ||
val numbers = mutableListOf(1, 3, 2, 5, 4) | ||
val nums = ArrayList<Int>(numbers) | ||
println("列表 nums = $nums") | ||
|
||
/* 访问元素 */ | ||
val num = nums[1] | ||
println("访问索引 1 处的元素,得到 num = $num") | ||
|
||
/* 更新元素 */ | ||
nums[1] = 0 | ||
println("将索引 1 处的元素更新为 0 ,得到 nums = $nums") | ||
|
||
/* 清空列表 */ | ||
nums.clear() | ||
println("清空列表后 nums = $nums") | ||
|
||
/* 在尾部添加元素 */ | ||
nums.add(1) | ||
nums.add(3) | ||
nums.add(2) | ||
nums.add(5) | ||
nums.add(4) | ||
println("添加元素后 nums = $nums") | ||
|
||
/* 在中间插入元素 */ | ||
nums.add(3, 6) | ||
println("在索引 3 处插入数字 6 ,得到 nums = $nums") | ||
|
||
/* 删除元素 */ | ||
nums.removeAt(3) | ||
println("删除索引 3 处的元素,得到 nums = $nums") | ||
|
||
/* 通过索引遍历列表 */ | ||
var count = 0 | ||
for (i in nums.indices) { | ||
count += nums[i] | ||
} | ||
|
||
/* 直接遍历列表元素 */ | ||
for (j in nums) { | ||
count += j | ||
} | ||
|
||
/* 拼接两个列表*/ | ||
val nums1 = ArrayList<Int>(listOf(6, 8, 7, 10, 9)) | ||
nums.addAll(nums1) | ||
println("将列表 nums1 拼接到 nums 之后,得到 nums = $nums") | ||
|
||
/* 排序列表 */ | ||
nums.sort() //排序后,列表元素从小到大排列 | ||
println("排序列表后 nums = $nums") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
/** | ||
* File: my_list.kt | ||
* Created Time: 2024-01-25 | ||
* Author: curtishd (1023632660@qq.com) | ||
*/ | ||
|
||
package chapter_array_and_linkedlist | ||
|
||
/* 列表类 */ | ||
class MyList { | ||
private var arr: IntArray = intArrayOf() // 数组(存储列表元素) | ||
private var capacity = 10 // 列表容量 | ||
private var size = 0 // 列表长度(当前元素数量) | ||
private var extendRatio = 2 // 每次列表扩容的倍数 | ||
|
||
/* 构造函数 */ | ||
init { | ||
arr = IntArray(capacity) | ||
} | ||
|
||
/* 获取列表长度(当前元素数量) */ | ||
fun size(): Int { | ||
return size | ||
} | ||
|
||
/* 获取列表容量 */ | ||
fun capacity(): Int { | ||
return capacity | ||
} | ||
|
||
/* 访问元素 */ | ||
fun get(index: Int): Int { | ||
// 索引如果越界,则抛出异常,下同 | ||
if (index < 0 || index >= size) | ||
throw IndexOutOfBoundsException() | ||
return arr[index] | ||
} | ||
|
||
/* 更新元素 */ | ||
fun set(index: Int, num: Int) { | ||
if (index < 0 || index >= size) | ||
throw IndexOutOfBoundsException("索引越界") | ||
arr[index] = num | ||
} | ||
|
||
/* 在尾部添加元素 */ | ||
fun add(num: Int) { | ||
// 元素数量超出容量时,触发扩容机制 | ||
if (size == capacity()) | ||
extendCapacity() | ||
arr[size] = num | ||
// 更新元素数量 | ||
size++ | ||
} | ||
|
||
/* 在中间插入元素 */ | ||
fun insert(index: Int, num: Int) { | ||
if (index < 0 || index >= size) | ||
throw IndexOutOfBoundsException("索引越界") | ||
// 元素数量超出容量时,触发扩容机制 | ||
if (size == capacity()) | ||
extendCapacity() | ||
// 将索引 index 以及之后的元素都向后移动一位 | ||
for (j in size - 1 downTo index) | ||
arr[j + 1] = arr[j] | ||
arr[index] = num | ||
// 更新元素数量 | ||
size++ | ||
} | ||
|
||
/* 删除元素 */ | ||
fun remove(index: Int): Int { | ||
if (index < 0 || index >= size) | ||
throw IndexOutOfBoundsException("索引越界") | ||
val num: Int = arr[index] | ||
// 将将索引 index 之后的元素都向前移动一位 | ||
for (j in index..<size - 1) | ||
arr[j] = arr[j + 1] | ||
// 更新元素数量 | ||
size-- | ||
// 返回被删除的元素 | ||
return num | ||
} | ||
|
||
/* 列表扩容 */ | ||
fun extendCapacity() { | ||
// 新建一个长度为原数组 extendRatio 倍的新数组,并将原数组复制到新数组 | ||
arr = arr.copyOf(capacity() * extendRatio) | ||
// 更新列表容量 | ||
capacity = arr.size | ||
} | ||
|
||
/* 将列表转换为数组 */ | ||
fun toArray(): IntArray { | ||
val size = size() | ||
// 仅转换有效长度范围内的列表元素 | ||
val arr = IntArray(size) | ||
for (i in 0..<size) { | ||
arr[i] = get(i) | ||
} | ||
return arr | ||
} | ||
} | ||
|
||
/* Driver Code */ | ||
fun main() { | ||
/* 初始化列表 */ | ||
val nums = MyList() | ||
/* 在尾部添加元素 */ | ||
nums.add(1) | ||
nums.add(3) | ||
nums.add(2) | ||
nums.add(5) | ||
nums.add(4) | ||
println("列表 nums = ${nums.toArray().contentToString()},容量 = ${nums.capacity()},长度 = ${nums.size()}") | ||
|
||
/* 在中间插入元素 */ | ||
nums.insert(3, 6) | ||
println("在索引 3 处插入数字 6 ,得到 nums = ${nums.toArray().contentToString()}") | ||
|
||
/* 删除元素 */ | ||
nums.remove(3) | ||
println("删除索引 3 处的元素,得到 nums = ${nums.toArray().contentToString()}") | ||
|
||
/* 访问元素 */ | ||
val num = nums.get(1) | ||
println("访问索引 1 处的元素,得到 num = $num") | ||
|
||
/* 更新元素 */ | ||
nums.set(1, 0) | ||
println("将索引 1 处的元素更新为 0 ,得到 nums = ${nums.toArray().contentToString()}") | ||
|
||
/* 测试扩容机制 */ | ||
for (i in 0..9) { | ||
// 在 i = 5 时,列表长度将超出列表容量,此时触发扩容机制 | ||
nums.add(i) | ||
} | ||
println("扩容后的列表 nums = ${nums.toArray().contentToString()},容量 = ${nums.capacity()},长度 = ${nums.size()}") | ||
} |