refactor: 增加代码示例,代码格式化。

This commit is contained in:
guoli 2022-12-21 19:28:07 +08:00
parent 3b979f8d4e
commit 114fc656d2
3 changed files with 522 additions and 209 deletions

View File

@ -0,0 +1,158 @@
// File: time_complexity.js
// Created Time: 2022-12-21
// Author: leeguooooo (leeguooooo@gmail.com)
/* 常数阶 */
function constant(n) {
let count = 0
const size = 100000
for (let i = 0; i < size; i++) {
count++
}
return count
}
/* 线性阶 */
function linear(n) {
let count = 0
for (let i = 0; i < n; i++) {
count++
}
return count
}
/* 线性阶(遍历数组) */
function arrayTraversal(nums) {
let count = 0
// 循环次数与数组长度成正比
for (let num of nums) {
count++
}
return count
}
/* 平方阶 */
function quadratic(n) {
let count = 0
// 循环次数与数组长度成平方关系
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
count++
}
}
return count
}
/* 平方阶(冒泡排序) */
function bubbleSort(nums) {
let count = 0 // 计数器
// 外循环:待排序元素数量为 n-1, n-2, ..., 1
for (let i = nums.length - 1; i > 0; i--) {
// 内循环:冒泡操作
for (let j = 0; j < i; j++) {
if (nums[j] > nums[j + 1]) {
// 交换 nums[j] 与 nums[j + 1]
let tmp = nums[j]
nums[j] = nums[j + 1]
nums[j + 1] = tmp
count += 3 // 元素交换包含 3 个单元操作
}
}
}
return count
}
/* 指数阶(循环实现) */
function exponential(n) {
let count = 0,
base = 1
// cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)
for (let i = 0; i < n; i++) {
for (let j = 0; j < base; j++) {
count++
}
base *= 2
}
// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1
return count
}
/* 指数阶(递归实现)*/
function expRecur(n) {
if (n == 1) return 1
return expRecur(n - 1) + expRecur(n - 1) + 1
}
/* 对数阶(循环实现) */
function logarithmic(n) {
let count = 0
while (n > 1) {
n = n / 2
count++
}
return count
}
/* 对数阶(递归实现)*/
function logRecur(n) {
if (n <= 1) return 0
return logRecur(n / 2) + 1
}
/* 线性对数阶 */
function linearLogRecur(n) {
if (n <= 1) return 1
let count = linearLogRecur(n / 2) + linearLogRecur(n / 2)
for (let i = 0; i < n; i++) {
count++
}
return count
}
/* 阶乘阶(递归实现) */
function factorialRecur(n) {
if (n == 0) return 1
let count = 0
// 从 1 个分裂出 n 个
for (let i = 0; i < n; i++) {
count += factorialRecur(n - 1)
}
return count
}
/* 生成一个数组,元素为 { 1, 2, ..., n },顺序被打乱 */
function randomNumbers(n) {
// 生成数组 nums = { 1, 2, 3, ..., n }
let nums = [...Array(n).keys()].map(i => i + 1);
shuffle(nums);
return nums;
}
/* 查找数组 nums 中数字 1 所在索引 */
function findOne(nums) {
for (let i = 0; i < nums.length; i++) {
if (nums[i] === 1) return i;
}
return -1;
}
/* 随机打乱数组元素 */
function shuffle(arr) {
let n = arr.length;
for (let i = 0; i < n; i++) {
let j = Math.floor(Math.random() * n);
[arr[i], arr[j]] = [arr[j], arr[i]];
}
}
/* Driver Code */
; (function main() {
for (let i = 0; i < 10; i++) {
let n = 100;
let nums = randomNumbers(n);
let index = findOne(nums);
console.log("\n数组 [ 1, 2, ..., n ] 被打乱后 =", nums);
console.log("数字 1 的索引为", index);
}
})()

View File

@ -0,0 +1,157 @@
// File: time_complexity.ts
// Created Time: 2022-12-21
// Author: leeguooooo (leeguooooo@gmail.com)
/* 常数阶 */
function constant(n: number): number {
let count = 0
const size = 100000
for (let i = 0; i < size; i++) {
count++
}
return count
}
/* 线性阶 */
function linear(n: number): number {
let count = 0
for (let i = 0; i < n; i++) {
count++
}
return count
}
/* 线性阶(遍历数组) */
function arrayTraversal(nums: number[]) {
let count = 0
// 循环次数与数组长度成正比
for (let num of nums) {
count++
}
return count
}
/* 平方阶 */
function quadratic(n: number) {
let count = 0
// 循环次数与数组长度成平方关系
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
count++
}
}
return count
}
/* 平方阶(冒泡排序) */
function bubbleSort(nums: number[]) {
let count = 0 // 计数器
// 外循环:待排序元素数量为 n-1, n-2, ..., 1
for (let i = nums.length - 1; i > 0; i--) {
// 内循环:冒泡操作
for (let j = 0; j < i; j++) {
if (nums[j] > nums[j + 1]) {
// 交换 nums[j] 与 nums[j + 1]
let tmp = nums[j]
nums[j] = nums[j + 1]
nums[j + 1] = tmp
count += 3 // 元素交换包含 3 个单元操作
}
}
}
return count
}
/* 指数阶(循环实现) */
function exponential(n: number) {
let count = 0,
base = 1
// cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)
for (let i = 0; i < n; i++) {
for (let j = 0; j < base; j++) {
count++
}
base *= 2
}
// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1
return count
}
/* 指数阶(递归实现)*/
function expRecur(n: number) {
if (n == 1) return 1
return expRecur(n - 1) + expRecur(n - 1) + 1
}
/* 对数阶(循环实现) */
function logarithmic(n: number) {
let count = 0
while (n > 1) {
n = n / 2
count++
}
return count
}
/* 对数阶(递归实现)*/
function logRecur(n: number) {
if (n <= 1) return 0
return logRecur(n / 2) + 1
}
/* 线性对数阶 */
function linearLogRecur(n: number) {
if (n <= 1) return 1
let count = linearLogRecur(n / 2) + linearLogRecur(n / 2)
for (let i = 0; i < n; i++) {
count++
}
return count
}
/* 阶乘阶(递归实现) */
function factorialRecur(n: number) {
if (n == 0) return 1
let count = 0
// 从 1 个分裂出 n 个
for (let i = 0; i < n; i++) {
count += factorialRecur(n - 1)
}
return count
}
/* 生成一个数组,元素为 { 1, 2, ..., n },顺序被打乱 */
function randomNumbers(n: number): number[] {
// 生成数组 nums = { 1, 2, 3, ..., n }
let nums = [...Array(n).keys()].map((i) => i + 1)
shuffle(nums)
return nums
}
/* 查找数组 nums 中数字 1 所在索引 */
function findOne(nums: number[]): number {
for (let i = 0; i < nums.length; i++) {
if (nums[i] === 1) return i
}
return -1
}
/* 随机打乱数组元素 */
function shuffle(arr: number[]) {
let n = arr.length
for (let i = 0; i < n; i++) {
let j = Math.floor(Math.random() * n)
;[arr[i], arr[j]] = [arr[j], arr[i]]
}
}
/* Driver Code */
; (function main() {
for (let i = 0; i < 10; i++) {
let n = 100
let nums = randomNumbers(n)
let index = findOne(nums)
console.log('\n数组 [ 1, 2, ..., n ] 被打乱后 =', nums)
console.log('数字 1 的索引为', index)
}
})()

View File

@ -96,13 +96,13 @@ $$
```typescript title="" ```typescript title=""
// 在某运行平台下 // 在某运行平台下
function algorithm(n: number): void { function algorithm(n: number): void {
let a = 2 // 1 ns let a = 2 // 1 ns
a = a + 1 // 1 ns a = a + 1 // 1 ns
a = a * 2 // 10 ns a = a * 2 // 10 ns
// 循环 n 次 // 循环 n 次
for (let i = 0; i < n; i++) { // 1 ns for (let i = 0; i < n; i++) { // 1 ns
a = a + i // 5 ns a = a + i // 5 ns
} }
} }
``` ```
@ -237,21 +237,21 @@ $$
```typescript title="" ```typescript title=""
// 算法 A 时间复杂度:常数阶 // 算法 A 时间复杂度:常数阶
function algorithmA(n: number) { function algorithmA(n: number) {
console.log(0) console.log(0)
} }
// 算法 A 时间复杂度:常数阶 // 算法 A 时间复杂度:常数阶
function algorithmB(n: number) { function algorithmB(n: number) {
for (let i = 0; i < n; i++) { for (let i = 0; i < n; i++) {
console.log(0) console.log(0)
} }
} }
// 算法 C 时间复杂度:常数阶 // 算法 C 时间复杂度:常数阶
function algorithmC(n: number) { function algorithmC(n: number) {
for (let i = 0; i < 1000000; i++) { for (let i = 0; i < 1000000; i++) {
console.log(0) console.log(0)
} }
} }
``` ```
@ -337,7 +337,7 @@ $$
// 循环 n 次 // 循环 n 次
for i := 0; i < n; i++ { // +1 for i := 0; i < n; i++ { // +1
fmt.Println(a) // +1 fmt.Println(a) // +1
} }
} }
``` ```
@ -345,14 +345,13 @@ $$
```js title="" ```js title=""
function algorithm(n) { function algorithm(n) {
let a = 1 // +1 let a = 1 // +1
a = a + 1 // +1 a = a + 1 // +1
a = a * 2 // +1 a = a * 2 // +1
// 循环 n 次 // 循环 n 次
for (let i = 0; i < n; i++) { for (let i = 0; i < n; i++) { // +1每轮都执行 i ++
// +1每轮都执行 i ++ console.log(0) // +1
console.log(0) // +1 }
}
} }
``` ```
@ -360,14 +359,13 @@ $$
```typescript title="" ```typescript title=""
function algorithm(n: number) { function algorithm(n: number) {
let a = 1 // +1 let a = 1 // +1
a = a + 1 // +1 a = a + 1 // +1
a = a * 2 // +1 a = a * 2 // +1
// 循环 n 次 // 循环 n 次
for (let i = 0; i < n; i++) { for (let i = 0; i < n; i++) { // +1每轮都执行 i ++
// +1每轮都执行 i ++ console.log(0) // +1
console.log(0) // +1 }
}
} }
``` ```
@ -395,7 +393,7 @@ $T(n)$ 是个一次函数,说明时间增长趋势是线性的,因此易得
$$ $$
T(n) \leq c \cdot f(n) T(n) \leq c \cdot f(n)
$$ $$
则可认为 $f(n)$ 给出了 $T(n)$ 的一个渐近上界,记为 则可认为 $f(n)$ 给出了 $T(n)$ 的一个渐近上界,记为
$$ $$
T(n) = O(f(n)) T(n) = O(f(n))
$$ $$
@ -510,18 +508,18 @@ $$
```js title="" ```js title=""
function algorithm(n) { function algorithm(n) {
let a = 1 // +0技巧 1 let a = 1 // +0技巧 1
a = a + n // +0技巧 1 a = a + n // +0技巧 1
// +n技巧 2 // +n技巧 2
for (let i = 0; i < 5 * n + 1; i++) { for (let i = 0; i < 5 * n + 1; i++) {
console.log(0) console.log(0)
} }
// +n*n技巧 3 // +n*n技巧 3
for (let i = 0; i < 2 * n; i++) { for (let i = 0; i < 2 * n; i++) {
for (let j = 0; j < n + 1; j++) { for (let j = 0; j < n + 1; j++) {
console.log(0) console.log(0)
}
} }
}
} }
``` ```
@ -529,18 +527,18 @@ $$
```typescript title="" ```typescript title=""
function algorithm(n: number) { function algorithm(n: number) {
let a = 1 // +0技巧 1 let a = 1 // +0技巧 1
a = a + n // +0技巧 1 a = a + n // +0技巧 1
// +n技巧 2 // +n技巧 2
for (let i = 0; i < 5 * n + 1; i++) { for (let i = 0; i < 5 * n + 1; i++) {
console.log(0) console.log(0)
} }
// +n*n技巧 3 // +n*n技巧 3
for (let i = 0; i < 2 * n; i++) { for (let i = 0; i < 2 * n; i++) {
for (let j = 0; j < n + 1; j++) { for (let j = 0; j < n + 1; j++) {
console.log(0) console.log(0)
}
} }
}
} }
``` ```
@ -656,12 +654,12 @@ $$
```js title="time_complexity.js" ```js title="time_complexity.js"
/* 常数阶 */ /* 常数阶 */
function constant(n) { function constant(n) {
let count = 0 let count = 0
const size = 100000 const size = 100000
for (let i = 0; i < size; i++) { for (let i = 0; i < size; i++) {
count++ count++
} }
return count return count
} }
``` ```
@ -670,12 +668,12 @@ $$
```typescript title="time_complexity.ts" ```typescript title="time_complexity.ts"
/* 常数阶 */ /* 常数阶 */
function constant(n: number): number { function constant(n: number): number {
let count = 0 let count = 0
const size = 100000 const size = 100000
for (let i = 0; i < size; i++) { for (let i = 0; i < size; i++) {
count++ count++
} }
return count return count
} }
``` ```
@ -748,11 +746,11 @@ $$
```js title="time_complexity.js" ```js title="time_complexity.js"
/* 线性阶 */ /* 线性阶 */
function linear(n) { function linear(n) {
let count = 0 let count = 0
for (let i = 0; i < n; i++) { for (let i = 0; i < n; i++) {
count++ count++
} }
return count return count
} }
``` ```
@ -761,11 +759,11 @@ $$
```typescript title="time_complexity.ts" ```typescript title="time_complexity.ts"
/* 线性阶 */ /* 线性阶 */
function linear(n: number): number { function linear(n: number): number {
let count = 0 let count = 0
for (let i = 0; i < n; i++) { for (let i = 0; i < n; i++) {
count++ count++
} }
return count return count
} }
``` ```
@ -846,12 +844,12 @@ $$
```js title="time_complexity.js" ```js title="time_complexity.js"
/* 线性阶(遍历数组) */ /* 线性阶(遍历数组) */
function arrayTraversal(nums) { function arrayTraversal(nums) {
let count = 0 let count = 0
// 循环次数与数组长度成正比 // 循环次数与数组长度成正比
for (let num of nums) { for (let num of nums) {
count++ count++
} }
return count return count
} }
``` ```
@ -860,12 +858,12 @@ $$
```typescript title="time_complexity.ts" ```typescript title="time_complexity.ts"
/* 线性阶(遍历数组) */ /* 线性阶(遍历数组) */
function arrayTraversal(nums: number[]) { function arrayTraversal(nums: number[]) {
let count = 0 let count = 0
// 循环次数与数组长度成正比 // 循环次数与数组长度成正比
for (let num of nums) { for (let num of nums) {
count++ count++
} }
return count return count
} }
``` ```
@ -951,14 +949,14 @@ $$
```js title="time_complexity.js" ```js title="time_complexity.js"
/* 平方阶 */ /* 平方阶 */
function quadratic(n) { function quadratic(n) {
let count = 0 let count = 0
// 循环次数与数组长度成平方关系 // 循环次数与数组长度成平方关系
for (let i = 0; i < n; i++) { for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) { for (let j = 0; j < n; j++) {
count++ count++
}
} }
} return count
return count
} }
``` ```
@ -967,14 +965,14 @@ $$
```typescript title="time_complexity.ts" ```typescript title="time_complexity.ts"
/* 平方阶 */ /* 平方阶 */
function quadratic(n: number) { function quadratic(n: number) {
let count = 0 let count = 0
// 循环次数与数组长度成平方关系 // 循环次数与数组长度成平方关系
for (let i = 0; i < n; i++) { for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) { for (let j = 0; j < n; j++) {
count++ count++
}
} }
} return count
return count
} }
``` ```
@ -1093,21 +1091,21 @@ $$
```js title="time_complexity.js" ```js title="time_complexity.js"
/* 平方阶(冒泡排序) */ /* 平方阶(冒泡排序) */
function bubbleSort(nums) { function bubbleSort(nums) {
let count = 0 // 计数器 let count = 0 // 计数器
// 外循环:待排序元素数量为 n-1, n-2, ..., 1 // 外循环:待排序元素数量为 n-1, n-2, ..., 1
for (let i = nums.length - 1; i > 0; i--) { for (let i = nums.length - 1; i > 0; i--) {
// 内循环:冒泡操作 // 内循环:冒泡操作
for (let j = 0; j < i; j++) { for (let j = 0; j < i; j++) {
if (nums[j] > nums[j + 1]) { if (nums[j] > nums[j + 1]) {
// 交换 nums[j] 与 nums[j + 1] // 交换 nums[j] 与 nums[j + 1]
let tmp = nums[j] let tmp = nums[j]
nums[j] = nums[j + 1] nums[j] = nums[j + 1]
nums[j + 1] = tmp nums[j + 1] = tmp
count += 3 // 元素交换包含 3 个单元操作 count += 3 // 元素交换包含 3 个单元操作
} }
}
} }
} return count
return count
} }
``` ```
@ -1228,17 +1226,17 @@ $$
```js title="time_complexity.js" ```js title="time_complexity.js"
/* 指数阶(循环实现) */ /* 指数阶(循环实现) */
function exponential(n) { function exponential(n) {
let count = 0, let count = 0,
base = 1 base = 1
// cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1) // cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)
for (let i = 0; i < n; i++) { for (let i = 0; i < n; i++) {
for (let j = 0; j < base; j++) { for (let j = 0; j < base; j++) {
count++ count++
}
base *= 2
} }
base *= 2 // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1
} return count
// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1
return count
} }
``` ```
@ -1247,17 +1245,17 @@ $$
```typescript title="time_complexity.ts" ```typescript title="time_complexity.ts"
/* 指数阶(循环实现) */ /* 指数阶(循环实现) */
function exponential(n: number) { function exponential(n: number) {
let count = 0, let count = 0,
base = 1 base = 1
// cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1) // cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)
for (let i = 0; i < n; i++) { for (let i = 0; i < n; i++) {
for (let j = 0; j < base; j++) { for (let j = 0; j < base; j++) {
count++ count++
}
base *= 2
} }
base *= 2 // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1
} return count
// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1
return count
} }
``` ```
@ -1325,8 +1323,8 @@ $$
```js title="time_complexity.js" ```js title="time_complexity.js"
/* 指数阶(递归实现)*/ /* 指数阶(递归实现)*/
function expRecur(n) { function expRecur(n) {
if (n == 1) return 1 if (n == 1) return 1
return expRecur(n - 1) + expRecur(n - 1) + 1 return expRecur(n - 1) + expRecur(n - 1) + 1
} }
``` ```
@ -1335,8 +1333,8 @@ $$
```typescript title="time_complexity.ts" ```typescript title="time_complexity.ts"
/* 指数阶(递归实现)*/ /* 指数阶(递归实现)*/
function expRecur(n: number) { function expRecur(n: number) {
if (n == 1) return 1 if (n == 1) return 1
return expRecur(n - 1) + expRecur(n - 1) + 1 return expRecur(n - 1) + expRecur(n - 1) + 1
} }
``` ```
@ -1419,12 +1417,12 @@ $$
```js title="time_complexity.js" ```js title="time_complexity.js"
/* 对数阶(循环实现) */ /* 对数阶(循环实现) */
function logarithmic(n) { function logarithmic(n) {
let count = 0 let count = 0
while (n > 1) { while (n > 1) {
n = n / 2 n = n / 2
count++ count++
} }
return count return count
} }
``` ```
@ -1433,12 +1431,12 @@ $$
```typescript title="time_complexity.ts" ```typescript title="time_complexity.ts"
/* 对数阶(循环实现) */ /* 对数阶(循环实现) */
function logarithmic(n: number) { function logarithmic(n: number) {
let count = 0 let count = 0
while (n > 1) { while (n > 1) {
n = n / 2 n = n / 2
count++ count++
} }
return count return count
} }
``` ```
@ -1506,8 +1504,8 @@ $$
```js title="time_complexity.js" ```js title="time_complexity.js"
/* 对数阶(递归实现)*/ /* 对数阶(递归实现)*/
function logRecur(n) { function logRecur(n) {
if (n <= 1) return 0 if (n <= 1) return 0
return logRecur(n / 2) + 1 return logRecur(n / 2) + 1
} }
``` ```
@ -1516,8 +1514,8 @@ $$
```typescript title="time_complexity.ts" ```typescript title="time_complexity.ts"
/* 对数阶(递归实现)*/ /* 对数阶(递归实现)*/
function logRecur(n: number) { function logRecur(n: number) {
if (n <= 1) return 0 if (n <= 1) return 0
return logRecur(n / 2) + 1 return logRecur(n / 2) + 1
} }
``` ```
@ -1545,7 +1543,7 @@ $$
/* 线性对数阶 */ /* 线性对数阶 */
int linearLogRecur(float n) { int linearLogRecur(float n) {
if (n <= 1) return 1; if (n <= 1) return 1;
int count = linearLogRecur(n / 2) + int count = linearLogRecur(n / 2) +
linearLogRecur(n / 2); linearLogRecur(n / 2);
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
count++; count++;
@ -1560,7 +1558,7 @@ $$
/* 线性对数阶 */ /* 线性对数阶 */
int linearLogRecur(float n) { int linearLogRecur(float n) {
if (n <= 1) return 1; if (n <= 1) return 1;
int count = linearLogRecur(n / 2) + int count = linearLogRecur(n / 2) +
linearLogRecur(n / 2); linearLogRecur(n / 2);
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
count++; count++;
@ -1604,12 +1602,12 @@ $$
```js title="time_complexity.js" ```js title="time_complexity.js"
/* 线性对数阶 */ /* 线性对数阶 */
function linearLogRecur(n) { function linearLogRecur(n) {
if (n <= 1) return 1 if (n <= 1) return 1
let count = linearLogRecur(n / 2) + linearLogRecur(n / 2) let count = linearLogRecur(n / 2) + linearLogRecur(n / 2)
for (let i = 0; i < n; i++) { for (let i = 0; i < n; i++) {
count++ count++
} }
return count return count
} }
``` ```
@ -1618,12 +1616,12 @@ $$
```typescript title="time_complexity.ts" ```typescript title="time_complexity.ts"
/* 线性对数阶 */ /* 线性对数阶 */
function linearLogRecur(n: number) { function linearLogRecur(n: number) {
if (n <= 1) return 1 if (n <= 1) return 1
let count = linearLogRecur(n / 2) + linearLogRecur(n / 2) let count = linearLogRecur(n / 2) + linearLogRecur(n / 2)
for (let i = 0; i < n; i++) { for (let i = 0; i < n; i++) {
count++ count++
} }
return count return count
} }
``` ```
@ -1718,13 +1716,13 @@ $$
```js title="time_complexity.js" ```js title="time_complexity.js"
/* 阶乘阶(递归实现) */ /* 阶乘阶(递归实现) */
function factorialRecur(n) { function factorialRecur(n) {
if (n == 0) return 1 if (n == 0) return 1
let count = 0 let count = 0
// 从 1 个分裂出 n 个 // 从 1 个分裂出 n 个
for (let i = 0; i < n; i++) { for (let i = 0; i < n; i++) {
count += factorialRecur(n - 1) count += factorialRecur(n - 1)
} }
return count return count
} }
``` ```
@ -1733,13 +1731,13 @@ $$
```typescript title="time_complexity.ts" ```typescript title="time_complexity.ts"
/* 阶乘阶(递归实现) */ /* 阶乘阶(递归实现) */
function factorialRecur(n: number) { function factorialRecur(n: number) {
if (n == 0) return 1 if (n == 0) return 1
let count = 0 let count = 0
// 从 1 个分裂出 n 个 // 从 1 个分裂出 n 个
for (let i = 0; i < n; i++) { for (let i = 0; i < n; i++) {
count += factorialRecur(n - 1) count += factorialRecur(n - 1)
} }
return count return count
} }
``` ```
@ -1788,7 +1786,7 @@ $$
} }
return res; return res;
} }
/* 查找数组 nums 中数字 1 所在索引 */ /* 查找数组 nums 中数字 1 所在索引 */
static int findOne(int[] nums) { static int findOne(int[] nums) {
for (int i = 0; i < nums.length; i++) { for (int i = 0; i < nums.length; i++) {
@ -1797,7 +1795,7 @@ $$
} }
return -1; return -1;
} }
/* Driver Code */ /* Driver Code */
public static void main(String[] args) { public static void main(String[] args) {
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {
@ -1857,7 +1855,7 @@ $$
```python title="worst_best_time_complexity.py" ```python title="worst_best_time_complexity.py"
""" 生成一个数组,元素为: 1, 2, ..., n ,顺序被打乱 """ """ 生成一个数组,元素为: 1, 2, ..., n ,顺序被打乱 """
def random_numbers(n): def random_numbers(n):
# 生成数组 nums =: 1, 2, 3, ..., n # 生成数组 nums =: 1, 2, 3, ..., n
nums = [i for i in range(1, n + 1)] nums = [i for i in range(1, n + 1)]
# 随机打乱数组元素 # 随机打乱数组元素
random.shuffle(nums) random.shuffle(nums)
@ -1948,7 +1946,7 @@ $$
} }
/* Driver Code */ /* Driver Code */
;(function main() { ; (function main() {
for (let i = 0; i < 10; i++) { for (let i = 0; i < 10; i++) {
let n = 100; let n = 100;
let nums = randomNumbers(n); let nums = randomNumbers(n);
@ -1964,38 +1962,38 @@ $$
```typescript title="worst_best_time_complexity.ts" ```typescript title="worst_best_time_complexity.ts"
/* 生成一个数组,元素为 { 1, 2, ..., n },顺序被打乱 */ /* 生成一个数组,元素为 { 1, 2, ..., n },顺序被打乱 */
function randomNumbers(n: number): number[] { function randomNumbers(n: number): number[] {
// 生成数组 nums = { 1, 2, 3, ..., n } // 生成数组 nums = { 1, 2, 3, ..., n }
let nums = [...Array(n).keys()].map((i) => i + 1) let nums = [...Array(n).keys()].map((i) => i + 1)
shuffle(nums) shuffle(nums)
return nums return nums
} }
/* 查找数组 nums 中数字 1 所在索引 */ /* 查找数组 nums 中数字 1 所在索引 */
function findOne(nums: number[]): number { function findOne(nums: number[]): number {
for (let i = 0; i < nums.length; i++) { for (let i = 0; i < nums.length; i++) {
if (nums[i] === 1) return i if (nums[i] === 1) return i
} }
return -1 return -1
} }
/* 随机打乱数组元素 */ /* 随机打乱数组元素 */
function shuffle(arr: number[]) { function shuffle(arr: number[]) {
let n = arr.length let n = arr.length
for (let i = 0; i < n; i++) { for (let i = 0; i < n; i++) {
let j = Math.floor(Math.random() * n) let j = Math.floor(Math.random() * n)
;[arr[i], arr[j]] = [arr[j], arr[i]] ;[arr[i], arr[j]] = [arr[j], arr[i]]
} }
} }
/* Driver Code */ /* Driver Code */
;(function main() { ; (function main() {
for (let i = 0; i < 10; i++) { for (let i = 0; i < 10; i++) {
let n = 100 let n = 100
let nums = randomNumbers(n) let nums = randomNumbers(n)
let index = findOne(nums) let index = findOne(nums)
console.log('\n数组 [ 1, 2, ..., n ] 被打乱后 =', nums) console.log('\n数组 [ 1, 2, ..., n ] 被打乱后 =', nums)
console.log('数字 1 的索引为', index) console.log('数字 1 的索引为', index)
} }
})() })()
``` ```