mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-12 15:36:05 +00:00
build
This commit is contained in:
@@ -1992,6 +1992,7 @@ Therefore, **we can use an explicit stack to simulate the behavior of the call s
|
||||
var res = 0
|
||||
// 递: 递归调用
|
||||
for (i in n downTo 0) {
|
||||
// 通过“入栈操作”模拟“递”
|
||||
stack.push(i)
|
||||
}
|
||||
// 归: 返回结果
|
||||
|
||||
@@ -1319,7 +1319,6 @@ Linear order indicates the number of operations grows linearly with the input da
|
||||
/* 线性阶 */
|
||||
fun linear(n: Int): Int {
|
||||
var count = 0
|
||||
// 循环次数与数组长度成正比
|
||||
for (i in 0..<n)
|
||||
count++
|
||||
return count
|
||||
@@ -2063,7 +2062,9 @@ For instance, in bubble sort, the outer loop runs $n - 1$ times, and the inner l
|
||||
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 个单元操作
|
||||
}
|
||||
}
|
||||
@@ -2340,8 +2341,8 @@ The following image and code simulate the cell division process, with a time com
|
||||
/* 指数阶(循环实现) */
|
||||
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++
|
||||
@@ -3747,12 +3748,11 @@ The "worst-case time complexity" corresponds to the asymptotic upper bound, deno
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user