Improve the consistency between code and comments in Kotlin (#1268)

* style(kotlin): Make code and comments consistent.

* style(kotlin): convert comment location.

* style(c): Add missing comment.

* style(kotlin): Remove redundant semicolon, parenthesis and brace

* style(kotlin): Put constants inside the function.

* style(kotlin): fix unnecessary indentation.

* style(swift): Add missing comment.

* style(kotlin): Add missing comment.

* style(kotlin): Remove redundant comment.

* style(kotlin): Add missing comment.

* Update linked_list.kt

* style(csharp,js,ts): Add missing comment.

* style(kotlin): Remove empty lines.

* Update list.cs

* Update list.js

* Update list.ts

* roll back to commit 1

* style(cs,js,ts): Add missing comment in docfile.

* style(kotlin): Use normal element swapping instead of scope functions.
This commit is contained in:
curtishd
2024-04-13 20:09:39 +08:00
committed by GitHub
parent b0d6fa0f38
commit 4d9bbe72e1
25 changed files with 74 additions and 61 deletions

View File

@@ -26,6 +26,7 @@ fun forLoopRecur(n: Int): Int {
var res = 0
// 递: 递归调用
for (i in n downTo 0) {
// 通过“入栈操作”模拟“递”
stack.push(i)
}
// 归: 返回结果

View File

@@ -18,7 +18,6 @@ fun constant(n: Int): Int {
/* 线性阶 */
fun linear(n: Int): Int {
var count = 0
// 循环次数与数组长度成正比
for (i in 0..<n)
count++
return count
@@ -55,7 +54,9 @@ fun bubbleSort(nums: IntArray): Int {
for (j in 0..<i) {
if (nums[j] > nums[j + 1]) {
// 交换 nums[j] 与 nums[j + 1]
nums[j] = nums[j + 1].also { nums[j + 1] = nums[j] }
val temp = nums[j]
nums[j] = nums[j + 1]
nums[j + 1] = temp
count += 3 // 元素交换包含 3 个单元操作
}
}
@@ -66,8 +67,8 @@ fun bubbleSort(nums: IntArray): Int {
/* 指数阶(循环实现) */
fun exponential(n: Int): Int {
var count = 0
// 细胞每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)
var base = 1
// 细胞每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)
for (i in 0..<n) {
for (j in 0..<base) {
count++

View File

@@ -13,12 +13,11 @@ fun randomNumbers(n: Int): Array<Int?> {
for (i in 0..<n) {
nums[i] = i + 1
}
val mutableList = nums.toMutableList()
// 随机打乱数组元素
mutableList.shuffle()
nums.shuffle()
val res = arrayOfNulls<Int>(n)
for (i in 0..<n) {
res[i] = mutableList[i]
res[i] = nums[i]
}
return res
}