mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-24 10:33:34 +08:00
Simplify kotlin code and improve code readability (#1198)
* Add kotlin code block for chapter_hashing * Add kotlin code block for chapter_heap. * Add kotlin code block for chapter_stack_and_queue and chapter_tree * fix indentation * Update binary_tree.md * style(kotlin): simplify code and improve readability. * simplify kt code for chapter_computational_complexity. * style(kotlin): replace ArrayList with MutableList. * Update subset_sum_i.kt Use kotlin api instead of java. * Update subset_sum_ii.kt use kotlin api instead of java * style(kotlin): replace ArrayList with mutablelist. --------- Co-authored-by: Yudong Jin <krahets@163.com>
This commit is contained in:
@@ -55,7 +55,7 @@ fun traverse(nums: IntArray) {
|
||||
count += nums[i]
|
||||
}
|
||||
// 直接遍历数组元素
|
||||
for (j: Int in nums) {
|
||||
for (j in nums) {
|
||||
count += j
|
||||
}
|
||||
}
|
||||
@@ -63,7 +63,8 @@ fun traverse(nums: IntArray) {
|
||||
/* 在数组中查找指定元素 */
|
||||
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
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ package chapter_array_and_linkedlist
|
||||
import utils.ListNode
|
||||
import utils.printLinkedList
|
||||
|
||||
/* 在链表的节点 n0 之后插入节点p */
|
||||
/* 在链表的节点 n0 之后插入节点 P */
|
||||
fun insert(n0: ListNode?, p: ListNode?) {
|
||||
val n1 = n0?.next
|
||||
p?.next = n1
|
||||
@@ -18,16 +18,20 @@ fun insert(n0: ListNode?, p: ListNode?) {
|
||||
|
||||
/* 删除链表的节点 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
|
||||
}
|
||||
|
||||
/* 访问链表中索引为 index 的节点 */
|
||||
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
|
||||
}
|
||||
@@ -37,7 +41,8 @@ fun find(head: ListNode?, target: Int): Int {
|
||||
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++
|
||||
}
|
||||
@@ -46,6 +51,7 @@ fun find(head: ListNode?, target: Int): Int {
|
||||
|
||||
/* Driver Code */
|
||||
fun main() {
|
||||
/* 初始化链表 */
|
||||
// 初始化各个节点
|
||||
val n0 = ListNode(1)
|
||||
val n1 = ListNode(3)
|
||||
@@ -60,7 +66,8 @@ fun main() {
|
||||
n3.next = n4
|
||||
println("初始化的链表为")
|
||||
printLinkedList(n0)
|
||||
|
||||
|
||||
/* 插入节点 */
|
||||
insert(n0, ListNode(0))
|
||||
println("插入节点后的链表为")
|
||||
printLinkedList(n0)
|
||||
|
||||
@@ -8,9 +8,9 @@ package chapter_array_and_linkedlist
|
||||
|
||||
/* Driver Code */
|
||||
fun main() {
|
||||
/* 初始化列表 */
|
||||
// 可变集合
|
||||
val numbers = mutableListOf(1, 3, 2, 5, 4)
|
||||
val nums = ArrayList<Int>(numbers)
|
||||
val nums = mutableListOf(1, 3, 2, 5, 4)
|
||||
println("列表 nums = $nums")
|
||||
|
||||
/* 访问元素 */
|
||||
@@ -53,11 +53,11 @@ fun main() {
|
||||
}
|
||||
|
||||
/* 拼接两个列表*/
|
||||
val nums1 = ArrayList<Int>(listOf(6, 8, 7, 10, 9))
|
||||
val nums1 = mutableListOf(6, 8, 7, 10, 9)
|
||||
nums.addAll(nums1)
|
||||
println("将列表 nums1 拼接到 nums 之后,得到 nums = $nums")
|
||||
|
||||
/* 排序列表 */
|
||||
nums.sort() //排序后,列表元素从小到大排列
|
||||
nums.sort()
|
||||
println("排序列表后 nums = $nums")
|
||||
}
|
||||
@@ -9,11 +9,11 @@ package chapter_array_and_linkedlist
|
||||
/* 列表类 */
|
||||
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)
|
||||
}
|
||||
@@ -32,7 +32,7 @@ class MyList {
|
||||
fun get(index: Int): Int {
|
||||
// 索引如果越界,则抛出异常,下同
|
||||
if (index < 0 || index >= size)
|
||||
throw IndexOutOfBoundsException()
|
||||
throw IndexOutOfBoundsException("索引越界")
|
||||
return arr[index]
|
||||
}
|
||||
|
||||
@@ -72,7 +72,7 @@ class MyList {
|
||||
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