mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 12:58:42 +08:00
Refine kotlin code (#1241)
* style(kotlin): Improve kotlin codes readability. * remove redundant quotes. * style(kotlin): improve codes readability. * style(kotlin): refine kotlin codes. * Create kotlin.yml * Create kotlin.yml * Delete .github/workflows/kotlin * Delete .github/workflows/main.yml * Create kotlin.yml * Update kotlin.yml * Delete .github/workflows/kotlin.yml * Create hello_world_workflow.main.kts * Delete .github/workflows/hello_world_workflow.main.kts * remove empty line
This commit is contained in:
@ -7,10 +7,11 @@
|
||||
package chapter_stack_and_queue
|
||||
|
||||
/* 基于环形数组实现的双向队列 */
|
||||
/* 构造方法 */
|
||||
class ArrayDeque(capacity: Int) {
|
||||
private var nums = IntArray(capacity) // 用于存储双向队列元素的数组
|
||||
private var front = 0 // 队首指针,指向队首元素
|
||||
private var queSize = 0 // 双向队列长度
|
||||
private var nums: IntArray = IntArray(capacity) // 用于存储双向队列元素的数组
|
||||
private var front: Int = 0 // 队首指针,指向队首元素
|
||||
private var queSize: Int = 0 // 双向队列长度
|
||||
|
||||
/* 获取双向队列的容量 */
|
||||
fun capacity(): Int {
|
||||
@ -71,7 +72,7 @@ class ArrayDeque(capacity: Int) {
|
||||
return num
|
||||
}
|
||||
|
||||
/* 访问队尾元素 */
|
||||
/* 队尾出队 */
|
||||
fun popLast(): Int {
|
||||
val num = peekLast()
|
||||
queSize--
|
||||
|
||||
@ -8,9 +8,9 @@ package chapter_stack_and_queue
|
||||
|
||||
/* 基于环形数组实现的队列 */
|
||||
class ArrayQueue(capacity: Int) {
|
||||
private val nums = IntArray(capacity) // 用于存储队列元素的数组
|
||||
private var front = 0 // 队首指针,指向队首元素
|
||||
private var queSize = 0 // 队列长度
|
||||
private val nums: IntArray = IntArray(capacity) // 用于存储队列元素的数组
|
||||
private var front: Int = 0 // 队首指针,指向队首元素
|
||||
private var queSize: Int = 0 // 队列长度
|
||||
|
||||
/* 获取队列的容量 */
|
||||
fun capacity(): Int {
|
||||
|
||||
@ -9,7 +9,7 @@ package chapter_stack_and_queue
|
||||
/* 基于数组实现的栈 */
|
||||
class ArrayStack {
|
||||
// 初始化列表(动态数组)
|
||||
private val stack = ArrayList<Int>()
|
||||
private val stack = mutableListOf<Int>()
|
||||
|
||||
/* 获取栈的长度 */
|
||||
fun size(): Int {
|
||||
@ -40,7 +40,7 @@ class ArrayStack {
|
||||
|
||||
/* 将 List 转化为 Array 并返回 */
|
||||
fun toArray(): Array<Any> {
|
||||
return stack.toArray()
|
||||
return stack.toTypedArray()
|
||||
}
|
||||
}
|
||||
|
||||
@ -63,7 +63,7 @@ fun main() {
|
||||
|
||||
/* 元素出栈 */
|
||||
val pop = stack.pop()
|
||||
println("出栈元素 pop = ${pop},出栈后 stack = ${stack.toArray().contentToString()}")
|
||||
println("出栈元素 pop = $pop,出栈后 stack = ${stack.toArray().contentToString()}")
|
||||
|
||||
/* 获取栈的长度 */
|
||||
val size = stack.size()
|
||||
|
||||
@ -7,7 +7,7 @@
|
||||
package chapter_stack_and_queue
|
||||
|
||||
/* 双向链表节点 */
|
||||
class ListNode(var value: Int) {
|
||||
class ListNode(var _val: Int) {
|
||||
// 节点值
|
||||
var next: ListNode? = null // 后继节点引用
|
||||
var prev: ListNode? = null // 前驱节点引用
|
||||
@ -15,9 +15,9 @@ class ListNode(var value: Int) {
|
||||
|
||||
/* 基于双向链表实现的双向队列 */
|
||||
class LinkedListDeque {
|
||||
private var front: ListNode? = null // 头节点 front ,尾节点 rear
|
||||
private var rear: ListNode? = null
|
||||
private var queSize = 0 // 双向队列的长度
|
||||
private var front: ListNode? = null // 头节点 front
|
||||
private var rear: ListNode? = null // 尾节点 rear
|
||||
private var queSize: Int = 0 // 双向队列的长度
|
||||
|
||||
/* 获取双向队列的长度 */
|
||||
fun size(): Int {
|
||||
@ -64,12 +64,12 @@ class LinkedListDeque {
|
||||
|
||||
/* 出队操作 */
|
||||
fun pop(isFront: Boolean): Int {
|
||||
if (isEmpty()) throw IndexOutOfBoundsException()
|
||||
|
||||
if (isEmpty())
|
||||
throw IndexOutOfBoundsException()
|
||||
val value: Int
|
||||
// 队首出队操作
|
||||
if (isFront) {
|
||||
value = front!!.value // 暂存头节点值
|
||||
value = front!!._val // 暂存头节点值
|
||||
// 删除头节点
|
||||
val fNext = front!!.next
|
||||
if (fNext != null) {
|
||||
@ -79,7 +79,7 @@ class LinkedListDeque {
|
||||
front = fNext // 更新头节点
|
||||
// 队尾出队操作
|
||||
} else {
|
||||
value = rear!!.value // 暂存尾节点值
|
||||
value = rear!!._val // 暂存尾节点值
|
||||
// 删除尾节点
|
||||
val rPrev = rear!!.prev
|
||||
if (rPrev != null) {
|
||||
@ -104,17 +104,14 @@ class LinkedListDeque {
|
||||
|
||||
/* 访问队首元素 */
|
||||
fun peekFirst(): Int {
|
||||
if (isEmpty()) {
|
||||
throw IndexOutOfBoundsException()
|
||||
|
||||
}
|
||||
return front!!.value
|
||||
if (isEmpty()) throw IndexOutOfBoundsException()
|
||||
return front!!._val
|
||||
}
|
||||
|
||||
/* 访问队尾元素 */
|
||||
fun peekLast(): Int {
|
||||
if (isEmpty()) throw IndexOutOfBoundsException()
|
||||
return rear!!.value
|
||||
return rear!!._val
|
||||
}
|
||||
|
||||
/* 返回数组用于打印 */
|
||||
@ -122,7 +119,7 @@ class LinkedListDeque {
|
||||
var node = front
|
||||
val res = IntArray(size())
|
||||
for (i in res.indices) {
|
||||
res[i] = node!!.value
|
||||
res[i] = node!!._val
|
||||
node = node.next
|
||||
}
|
||||
return res
|
||||
@ -163,4 +160,4 @@ fun main() {
|
||||
/* 判断双向队列是否为空 */
|
||||
val isEmpty = deque.isEmpty()
|
||||
println("双向队列是否为空 = $isEmpty")
|
||||
}
|
||||
}
|
||||
|
||||
@ -52,7 +52,7 @@ class LinkedListQueue(
|
||||
/* 访问队首元素 */
|
||||
fun peek(): Int {
|
||||
if (isEmpty()) throw IndexOutOfBoundsException()
|
||||
return front!!.value
|
||||
return front!!._val
|
||||
}
|
||||
|
||||
/* 将链表转化为 Array 并返回 */
|
||||
@ -60,7 +60,7 @@ class LinkedListQueue(
|
||||
var node = front
|
||||
val res = IntArray(size())
|
||||
for (i in res.indices) {
|
||||
res[i] = node!!.value
|
||||
res[i] = node!!._val
|
||||
node = node.next
|
||||
}
|
||||
return res
|
||||
|
||||
@ -41,7 +41,7 @@ class LinkedListStack(
|
||||
/* 访问栈顶元素 */
|
||||
fun peek(): Int? {
|
||||
if (isEmpty()) throw IndexOutOfBoundsException()
|
||||
return stackPeek?.value
|
||||
return stackPeek?._val
|
||||
}
|
||||
|
||||
/* 将 List 转化为 Array 并返回 */
|
||||
@ -49,7 +49,7 @@ class LinkedListStack(
|
||||
var node = stackPeek
|
||||
val res = IntArray(size())
|
||||
for (i in res.size - 1 downTo 0) {
|
||||
res[i] = node?.value!!
|
||||
res[i] = node?._val!!
|
||||
node = node.next
|
||||
}
|
||||
return res
|
||||
|
||||
Reference in New Issue
Block a user