mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-16 00:46:06 +00:00
Many bug fixes and improvements (#1270)
* Add Ruby and Kotlin icons Add the avatar of @curtishd * Update README * Synchronize zh-hant and zh versions. * Translate the pythontutor blocks to traditional Chinese * Fix en/mkdocs.yml * Update the landing page of the en version. * Fix the Dockerfile * Refine the en landingpage * Fix en landing page * Reset the README.md
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._val == 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)
|
||||
@@ -72,7 +79,7 @@ fun main() {
|
||||
|
||||
/* 訪問節點 */
|
||||
val node: ListNode = access(n0, 3)!!
|
||||
println("鏈結串列中索引 3 處的節點的值 = ${node.value}")
|
||||
println("鏈結串列中索引 3 處的節點的值 = ${node._val}")
|
||||
|
||||
/* 查詢節點 */
|
||||
val index: Int = find(n0, 2)
|
||||
|
||||
@@ -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,9 +9,9 @@ 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 {
|
||||
@@ -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