Update time complexity and space complexity.
This commit is contained in:
parent
f0c6de961a
commit
bec787b751
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* File: space_complexity_types.cpp
|
||||
* File: space_complexity.cpp
|
||||
* Created Time: 2022-11-25
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* File: time_complexity_types.cpp
|
||||
* File: time_complexity.cpp
|
||||
* Created Time: 2022-11-25
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
@ -2,7 +2,7 @@
|
||||
// Created Time: 2022-12-13
|
||||
// Author: msk397 (machangxinq@gmail.com)
|
||||
|
||||
package time_complexity
|
||||
package chapter_computational_complexity
|
||||
|
||||
/* 常数阶 */
|
||||
func constant(n int) int {
|
@ -1,45 +0,0 @@
|
||||
// File: worst_best_time_complexity.go
|
||||
// Created Time: 2022-12-13
|
||||
// Author: msk397 (machangxinq@gmail.com)
|
||||
|
||||
package time_complexity
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
)
|
||||
|
||||
/* 生成一个数组,元素为 { 1, 2, ..., n },顺序被打乱 */
|
||||
func randomNumbers(n int) []int {
|
||||
nums := make([]int, n)
|
||||
// 生成数组 nums = { 1, 2, 3, ..., n }
|
||||
for i := 0; i < n; i++ {
|
||||
nums[i] = i + 1
|
||||
}
|
||||
// 随机打乱数组元素
|
||||
rand.Shuffle(len(nums), func(i, j int) {
|
||||
nums[i], nums[j] = nums[j], nums[i]
|
||||
})
|
||||
return nums
|
||||
}
|
||||
|
||||
/* 查找数组 nums 中数字 1 所在索引 */
|
||||
func findOne(nums []int) int {
|
||||
for i := 0; i < len(nums); i++ {
|
||||
if nums[i] == 1 {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
func main() {
|
||||
for i := 0; i < 10; i++ {
|
||||
n := 100
|
||||
nums := randomNumbers(n)
|
||||
index := findOne(nums)
|
||||
fmt.Println("\n数组 [ 1, 2, ..., n ] 被打乱后 =", nums)
|
||||
fmt.Println("数字 1 的索引为", index)
|
||||
}
|
||||
}
|
@ -2,7 +2,7 @@
|
||||
// Created Time: 2022-12-13
|
||||
// Author: msk397 (machangxinq@gmail.com)
|
||||
|
||||
package time_complexity
|
||||
package chapter_computational_complexity
|
||||
|
||||
import (
|
||||
"fmt"
|
@ -1,134 +0,0 @@
|
||||
// File: time_complexity_types.go
|
||||
// Created Time: 2022-12-13
|
||||
// Author: cathay (cathaycchen@gmail.com)
|
||||
|
||||
package chapter_computational_complexity
|
||||
|
||||
// constant 常数阶
|
||||
func constant(n int) int {
|
||||
count := 0
|
||||
var size = 100000
|
||||
for i := 0; i < size; i++ {
|
||||
count++
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
// linear 线性阶
|
||||
func linear(n int) int {
|
||||
count := 0
|
||||
for i := 0; i < n; i++ {
|
||||
count++
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
// arrayTraversal 线性阶(遍历数组)
|
||||
func arrayTraversal(nums []int) int {
|
||||
count := 0
|
||||
// 循环次数与数组长度成正比
|
||||
for range nums {
|
||||
count++
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
// quadratic 平方阶
|
||||
func quadratic(n int) int {
|
||||
count := 0
|
||||
// 循环次数与数组长度成平方关系
|
||||
for i := 0; i < n; i++ {
|
||||
for j := 0; j < n; j++ {
|
||||
count++
|
||||
}
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
// bubbleSort 平方阶(冒泡排序)
|
||||
func bubbleSort(nums []int) int {
|
||||
count := 0 // 计数器
|
||||
// 外循环:待排序元素数量为 n-1, n-2, ..., 1
|
||||
for i := len(nums) - 1; i > 0; i-- {
|
||||
// 内循环:冒泡操作
|
||||
for j := 0; j < i; j++ {
|
||||
if nums[j] > nums[j + 1] {
|
||||
// 交换 nums[j] 与 nums[j + 1]
|
||||
tmp := nums[j]
|
||||
nums[j] = nums[j + 1]
|
||||
nums[j + 1] = tmp
|
||||
count += 3 // 元素交换包含 3 个单元操作
|
||||
}
|
||||
}
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
// exponential 指数阶(循环实现)
|
||||
func exponential(n int) int {
|
||||
count := 0
|
||||
base := 1
|
||||
// cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)
|
||||
for i := 0; i < n; i++ {
|
||||
for j := 0; j < base; j++ {
|
||||
count++
|
||||
}
|
||||
base *= 2
|
||||
}
|
||||
// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1
|
||||
return count
|
||||
}
|
||||
|
||||
// expRecur 指数阶(递归实现)
|
||||
func expRecur(n int) int {
|
||||
if n == 1 {
|
||||
return 1
|
||||
}
|
||||
return expRecur(n - 1) + expRecur(n - 1) + 1
|
||||
}
|
||||
|
||||
// logarithmic 对数阶(循环实现)
|
||||
func logarithmic(n float32) int {
|
||||
count := 0
|
||||
for n > 1 {
|
||||
n = n / 2
|
||||
count++
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
// logRecur 对数阶(递归实现)
|
||||
func logRecur(n float32) int {
|
||||
if n <= 1 {
|
||||
return 0
|
||||
}
|
||||
return logRecur(n / 2) + 1
|
||||
}
|
||||
|
||||
// 线性对数阶
|
||||
func linearLogRecur(n float32) int {
|
||||
if n <= 1 {
|
||||
return 1
|
||||
}
|
||||
count := linearLogRecur(n / 2) + linearLogRecur(n / 2)
|
||||
for i := 0; float32(i) < n; i++ {
|
||||
count++
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
// factorialRecur 阶乘阶(递归实现)
|
||||
func factorialRecur(n int) int {
|
||||
if n == 0 {
|
||||
return 1
|
||||
}
|
||||
count := 0
|
||||
// 从 1 个分裂出 n 个
|
||||
for i := 0; i < n; i++ {
|
||||
count += factorialRecur(n - 1)
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
|
||||
|
@ -1,49 +0,0 @@
|
||||
// File: time_complexity_types_test.go
|
||||
// Created Time: 2022-12-13
|
||||
// Author: cathay (cathaycchen@gmail.com)
|
||||
|
||||
package chapter_computational_complexity
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRunCount(t *testing.T) {
|
||||
// ======= Test Case =======
|
||||
n := 8
|
||||
fmt.Println("输入数据大小 n =", n)
|
||||
|
||||
// ====== Driver Code ======
|
||||
count := constant(n)
|
||||
fmt.Println("常数阶的计算操作数量 =", count)
|
||||
|
||||
count = linear(n)
|
||||
fmt.Println("线性阶的计算操作数量 =", count)
|
||||
count = arrayTraversal(make([]int, n))
|
||||
fmt.Println("线性阶(遍历数组)的计算操作数量 =", count)
|
||||
|
||||
count = quadratic(n)
|
||||
fmt.Println("平方阶的计算操作数量 =", count)
|
||||
nums := make([]int, n)
|
||||
for i := 0; i < n; i++ {
|
||||
nums[i] = n - i // [n,n-1,...,2,1]
|
||||
}
|
||||
count = bubbleSort(nums)
|
||||
fmt.Println("平方阶(冒泡排序)的计算操作数量 =", count)
|
||||
|
||||
count = exponential(n)
|
||||
fmt.Println("指数阶(循环实现)的计算操作数量 =", count)
|
||||
count = expRecur(n)
|
||||
fmt.Println("指数阶(递归实现)的计算操作数量 =", count)
|
||||
|
||||
count = logarithmic(float32(n))
|
||||
fmt.Println("对数阶(循环实现)的计算操作数量 =", count)
|
||||
count = logRecur(float32(n))
|
||||
fmt.Println("对数阶(递归实现)的计算操作数量 =", count)
|
||||
count = linearLogRecur(float32(n))
|
||||
fmt.Println("线性对数阶(递归实现)的计算操作数量 =", count)
|
||||
|
||||
count = factorialRecur(n)
|
||||
fmt.Println("阶乘阶(递归实现)的计算操作数量 =", count)
|
||||
}
|
@ -1,12 +1,15 @@
|
||||
// File: worst_best_time_complexity.go
|
||||
// Created Time: 2022-12-13
|
||||
// Author: cathay (cathaycchen@gmail.com)
|
||||
// Author: msk397 (machangxinq@gmail.com), cathay (cathaycchen@gmail.com)
|
||||
|
||||
package chapter_computational_complexity
|
||||
|
||||
import "math/rand"
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
)
|
||||
|
||||
// randomNumbers 生成一个数组,元素为 { 1, 2, ..., n },顺序被打乱
|
||||
/* 生成一个数组,元素为 { 1, 2, ..., n },顺序被打乱 */
|
||||
func randomNumbers(n int) []int {
|
||||
nums := make([]int, n)
|
||||
// 生成数组 nums = { 1, 2, 3, ..., n }
|
||||
@ -20,7 +23,7 @@ func randomNumbers(n int) []int {
|
||||
return nums
|
||||
}
|
||||
|
||||
// findOne 查找数组 nums 中数字 1 所在索引
|
||||
/* 查找数组 nums 中数字 1 所在索引 */
|
||||
func findOne(nums []int) int {
|
||||
for i := 0; i < len(nums); i++ {
|
||||
if nums[i] == 1 {
|
||||
@ -29,3 +32,14 @@ func findOne(nums []int) int {
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
func main() {
|
||||
for i := 0; i < 10; i++ {
|
||||
n := 100
|
||||
nums := randomNumbers(n)
|
||||
index := findOne(nums)
|
||||
fmt.Println("\n数组 [ 1, 2, ..., n ] 被打乱后 =", nums)
|
||||
fmt.Println("数字 1 的索引为", index)
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
// Copyright 2022 Cathay. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
// File: worst_best_time_complexity.go
|
||||
// Created Time: 2022-12-13
|
||||
// Author: msk397 (machangxinq@gmail.com), cathay (cathaycchen@gmail.com)
|
||||
|
||||
package chapter_computational_complexity
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* File: space_complexity_types.java
|
||||
* File: space_complexity.java
|
||||
* Created Time: 2022-11-25
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
@ -9,7 +9,7 @@ package chapter_computational_complexity;
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
public class space_complexity_types {
|
||||
public class space_complexity {
|
||||
/* 函数 */
|
||||
static int function() {
|
||||
// do something
|
@ -1,12 +1,12 @@
|
||||
/*
|
||||
* File: time_complexity_types.java
|
||||
* File: time_complexity.java
|
||||
* Created Time: 2022-11-25
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package chapter_computational_complexity;
|
||||
|
||||
public class time_complexity_types {
|
||||
public class time_complexity {
|
||||
/* 常数阶 */
|
||||
static int constant(int n) {
|
||||
int count = 0;
|
@ -1,5 +1,5 @@
|
||||
'''
|
||||
File: space_complexity_types.py
|
||||
File: space_complexity.py
|
||||
Created Time: 2022-11-25
|
||||
Author: Krahets (krahets@163.com)
|
||||
'''
|
@ -1,5 +1,5 @@
|
||||
'''
|
||||
File: time_complexity_types.py
|
||||
File: time_complexity.py
|
||||
Created Time: 2022-11-25
|
||||
Author: Krahets (krahets@163.com)
|
||||
'''
|
@ -317,7 +317,7 @@ $$
|
||||
|
||||
=== "Java"
|
||||
|
||||
```java title="space_complexity_types.java"
|
||||
```java title="space_complexity.java"
|
||||
/* 常数阶 */
|
||||
void constant(int n) {
|
||||
// 常量、变量、对象占用 O(1) 空间
|
||||
@ -338,7 +338,7 @@ $$
|
||||
|
||||
=== "C++"
|
||||
|
||||
```cpp title="space_complexity_types.cpp"
|
||||
```cpp title="space_complexity.cpp"
|
||||
/* 常数阶 */
|
||||
void constant(int n) {
|
||||
// 常量、变量、对象占用 O(1) 空间
|
||||
@ -359,7 +359,7 @@ $$
|
||||
|
||||
=== "Python"
|
||||
|
||||
```python title="space_complexity_types.py"
|
||||
```python title="space_complexity.py"
|
||||
""" 常数阶 """
|
||||
def constant(n):
|
||||
# 常量、变量、对象占用 O(1) 空间
|
||||
@ -376,31 +376,31 @@ $$
|
||||
|
||||
=== "Go"
|
||||
|
||||
```go title="space_complexity_types.go"
|
||||
```go title="space_complexity.go"
|
||||
|
||||
```
|
||||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="space_complexity_types.js"
|
||||
```js title="space_complexity.js"
|
||||
|
||||
```
|
||||
|
||||
=== "TypeScript"
|
||||
|
||||
```typescript title="space_complexity_types.ts"
|
||||
```typescript title="space_complexity.ts"
|
||||
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="space_complexity_types.c"
|
||||
```c title="space_complexity.c"
|
||||
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="space_complexity_types.cs"
|
||||
```csharp title="space_complexity.cs"
|
||||
|
||||
```
|
||||
|
||||
@ -410,7 +410,7 @@ $$
|
||||
|
||||
=== "Java"
|
||||
|
||||
```java title="space_complexity_types.java"
|
||||
```java title="space_complexity.java"
|
||||
/* 线性阶 */
|
||||
void linear(int n) {
|
||||
// 长度为 n 的数组占用 O(n) 空间
|
||||
@ -430,7 +430,7 @@ $$
|
||||
|
||||
=== "C++"
|
||||
|
||||
```cpp title="space_complexity_types.cpp"
|
||||
```cpp title="space_complexity.cpp"
|
||||
/* 线性阶 */
|
||||
void linear(int n) {
|
||||
// 长度为 n 的数组占用 O(n) 空间
|
||||
@ -450,7 +450,7 @@ $$
|
||||
|
||||
=== "Python"
|
||||
|
||||
```python title="space_complexity_types.py"
|
||||
```python title="space_complexity.py"
|
||||
""" 线性阶 """
|
||||
def linear(n):
|
||||
# 长度为 n 的列表占用 O(n) 空间
|
||||
@ -463,31 +463,31 @@ $$
|
||||
|
||||
=== "Go"
|
||||
|
||||
```go title="space_complexity_types.go"
|
||||
```go title="space_complexity.go"
|
||||
|
||||
```
|
||||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="space_complexity_types.js"
|
||||
```js title="space_complexity.js"
|
||||
|
||||
```
|
||||
|
||||
=== "TypeScript"
|
||||
|
||||
```typescript title="space_complexity_types.ts"
|
||||
```typescript title="space_complexity.ts"
|
||||
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="space_complexity_types.c"
|
||||
```c title="space_complexity.c"
|
||||
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="space_complexity_types.cs"
|
||||
```csharp title="space_complexity.cs"
|
||||
|
||||
```
|
||||
|
||||
@ -495,7 +495,7 @@ $$
|
||||
|
||||
=== "Java"
|
||||
|
||||
```java title="space_complexity_types.java"
|
||||
```java title="space_complexity.java"
|
||||
/* 线性阶(递归实现) */
|
||||
void linearRecur(int n) {
|
||||
System.out.println("递归 n = " + n);
|
||||
@ -506,7 +506,7 @@ $$
|
||||
|
||||
=== "C++"
|
||||
|
||||
```cpp title="space_complexity_types.cpp"
|
||||
```cpp title="space_complexity.cpp"
|
||||
/* 线性阶(递归实现) */
|
||||
void linearRecur(int n) {
|
||||
cout << "递归 n = " << n << endl;
|
||||
@ -517,7 +517,7 @@ $$
|
||||
|
||||
=== "Python"
|
||||
|
||||
```python title="space_complexity_types.py"
|
||||
```python title="space_complexity.py"
|
||||
""" 线性阶(递归实现) """
|
||||
def linearRecur(n):
|
||||
print("递归 n =", n)
|
||||
@ -527,31 +527,31 @@ $$
|
||||
|
||||
=== "Go"
|
||||
|
||||
```go title="space_complexity_types.go"
|
||||
```go title="space_complexity.go"
|
||||
|
||||
```
|
||||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="space_complexity_types.js"
|
||||
```js title="space_complexity.js"
|
||||
|
||||
```
|
||||
|
||||
=== "TypeScript"
|
||||
|
||||
```typescript title="space_complexity_types.ts"
|
||||
```typescript title="space_complexity.ts"
|
||||
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="space_complexity_types.c"
|
||||
```c title="space_complexity.c"
|
||||
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="space_complexity_types.cs"
|
||||
```csharp title="space_complexity.cs"
|
||||
|
||||
```
|
||||
|
||||
@ -565,7 +565,7 @@ $$
|
||||
|
||||
=== "Java"
|
||||
|
||||
```java title="space_complexity_types.java"
|
||||
```java title="space_complexity.java"
|
||||
/* 平方阶 */
|
||||
void quadratic(int n) {
|
||||
// 矩阵占用 O(n^2) 空间
|
||||
@ -584,7 +584,7 @@ $$
|
||||
|
||||
=== "C++"
|
||||
|
||||
```cpp title="space_complexity_types.cpp"
|
||||
```cpp title="space_complexity.cpp"
|
||||
/* 平方阶 */
|
||||
void quadratic(int n) {
|
||||
// 二维列表占用 O(n^2) 空间
|
||||
@ -601,7 +601,7 @@ $$
|
||||
|
||||
=== "Python"
|
||||
|
||||
```python title="space_complexity_types.py"
|
||||
```python title="space_complexity.py"
|
||||
""" 平方阶 """
|
||||
def quadratic(n):
|
||||
# 二维列表占用 O(n^2) 空间
|
||||
@ -610,31 +610,31 @@ $$
|
||||
|
||||
=== "Go"
|
||||
|
||||
```go title="space_complexity_types.go"
|
||||
```go title="space_complexity.go"
|
||||
|
||||
```
|
||||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="space_complexity_types.js"
|
||||
```js title="space_complexity.js"
|
||||
|
||||
```
|
||||
|
||||
=== "TypeScript"
|
||||
|
||||
```typescript title="space_complexity_types.ts"
|
||||
```typescript title="space_complexity.ts"
|
||||
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="space_complexity_types.c"
|
||||
```c title="space_complexity.c"
|
||||
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="space_complexity_types.cs"
|
||||
```csharp title="space_complexity.cs"
|
||||
|
||||
```
|
||||
|
||||
@ -642,7 +642,7 @@ $$
|
||||
|
||||
=== "Java"
|
||||
|
||||
```java title="space_complexity_types.java"
|
||||
```java title="space_complexity.java"
|
||||
/* 平方阶(递归实现) */
|
||||
int quadraticRecur(int n) {
|
||||
if (n <= 0) return 0;
|
||||
@ -654,7 +654,7 @@ $$
|
||||
|
||||
=== "C++"
|
||||
|
||||
```cpp title="space_complexity_types.cpp"
|
||||
```cpp title="space_complexity.cpp"
|
||||
/* 平方阶(递归实现) */
|
||||
int quadraticRecur(int n) {
|
||||
if (n <= 0) return 0;
|
||||
@ -666,7 +666,7 @@ $$
|
||||
|
||||
=== "Python"
|
||||
|
||||
```python title="space_complexity_types.py"
|
||||
```python title="space_complexity.py"
|
||||
""" 平方阶(递归实现) """
|
||||
def quadratic_recur(n):
|
||||
if n <= 0: return 0
|
||||
@ -677,31 +677,31 @@ $$
|
||||
|
||||
=== "Go"
|
||||
|
||||
```go title="space_complexity_types.go"
|
||||
```go title="space_complexity.go"
|
||||
|
||||
```
|
||||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="space_complexity_types.js"
|
||||
```js title="space_complexity.js"
|
||||
|
||||
```
|
||||
|
||||
=== "TypeScript"
|
||||
|
||||
```typescript title="space_complexity_types.ts"
|
||||
```typescript title="space_complexity.ts"
|
||||
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="space_complexity_types.c"
|
||||
```c title="space_complexity.c"
|
||||
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="space_complexity_types.cs"
|
||||
```csharp title="space_complexity.cs"
|
||||
|
||||
```
|
||||
|
||||
@ -715,7 +715,7 @@ $$
|
||||
|
||||
=== "Java"
|
||||
|
||||
```java title="space_complexity_types.java"
|
||||
```java title="space_complexity.java"
|
||||
/* 指数阶(建立满二叉树) */
|
||||
TreeNode buildTree(int n) {
|
||||
if (n == 0) return null;
|
||||
@ -728,7 +728,7 @@ $$
|
||||
|
||||
=== "C++"
|
||||
|
||||
```cpp title="space_complexity_types.cpp"
|
||||
```cpp title="space_complexity.cpp"
|
||||
/* 指数阶(建立满二叉树) */
|
||||
TreeNode* buildTree(int n) {
|
||||
if (n == 0) return nullptr;
|
||||
@ -741,7 +741,7 @@ $$
|
||||
|
||||
=== "Python"
|
||||
|
||||
```python title="space_complexity_types.py"
|
||||
```python title="space_complexity.py"
|
||||
""" 指数阶(建立满二叉树) """
|
||||
def build_tree(n):
|
||||
if n == 0: return None
|
||||
@ -753,31 +753,31 @@ $$
|
||||
|
||||
=== "Go"
|
||||
|
||||
```go title="space_complexity_types.go"
|
||||
```go title="space_complexity.go"
|
||||
|
||||
```
|
||||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="space_complexity_types.js"
|
||||
```js title="space_complexity.js"
|
||||
|
||||
```
|
||||
|
||||
=== "TypeScript"
|
||||
|
||||
```typescript title="space_complexity_types.ts"
|
||||
```typescript title="space_complexity.ts"
|
||||
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="space_complexity_types.c"
|
||||
```c title="space_complexity.c"
|
||||
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="space_complexity_types.cs"
|
||||
```csharp title="space_complexity.cs"
|
||||
|
||||
```
|
||||
|
||||
|
@ -505,7 +505,7 @@ $$
|
||||
|
||||
=== "Java"
|
||||
|
||||
```java title="time_complexity_types.java"
|
||||
```java title="time_complexity.java"
|
||||
/* 常数阶 */
|
||||
int constant(int n) {
|
||||
int count = 0;
|
||||
@ -518,7 +518,7 @@ $$
|
||||
|
||||
=== "C++"
|
||||
|
||||
```cpp title="time_complexity_types.cpp"
|
||||
```cpp title="time_complexity.cpp"
|
||||
/* 常数阶 */
|
||||
int constant(int n) {
|
||||
int count = 0;
|
||||
@ -531,7 +531,7 @@ $$
|
||||
|
||||
=== "Python"
|
||||
|
||||
```python title="time_complexity_types.py"
|
||||
```python title="time_complexity.py"
|
||||
""" 常数阶 """
|
||||
def constant(n):
|
||||
count = 0
|
||||
@ -543,7 +543,7 @@ $$
|
||||
|
||||
=== "Go"
|
||||
|
||||
```go title="time_complexity_types.go"
|
||||
```go title="time_complexity.go"
|
||||
/* 常数阶 */
|
||||
func constant(n int) int {
|
||||
count := 0
|
||||
@ -557,25 +557,25 @@ $$
|
||||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="time_complexity_types.js"
|
||||
```js title="time_complexity.js"
|
||||
|
||||
```
|
||||
|
||||
=== "TypeScript"
|
||||
|
||||
```typescript title="time_complexity_types.ts"
|
||||
```typescript title="time_complexity.ts"
|
||||
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="time_complexity_types.c"
|
||||
```c title="time_complexity.c"
|
||||
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="time_complexity_types.cs"
|
||||
```csharp title="time_complexity.cs"
|
||||
|
||||
```
|
||||
|
||||
@ -585,7 +585,7 @@ $$
|
||||
|
||||
=== "Java"
|
||||
|
||||
```java title="time_complexity_types.java"
|
||||
```java title="time_complexity.java"
|
||||
/* 线性阶 */
|
||||
int linear(int n) {
|
||||
int count = 0;
|
||||
@ -597,7 +597,7 @@ $$
|
||||
|
||||
=== "C++"
|
||||
|
||||
```cpp title="time_complexity_types.cpp"
|
||||
```cpp title="time_complexity.cpp"
|
||||
/* 线性阶 */
|
||||
int linear(int n) {
|
||||
int count = 0;
|
||||
@ -609,7 +609,7 @@ $$
|
||||
|
||||
=== "Python"
|
||||
|
||||
```python title="time_complexity_types.py"
|
||||
```python title="time_complexity.py"
|
||||
""" 线性阶 """
|
||||
def linear(n):
|
||||
count = 0
|
||||
@ -620,7 +620,7 @@ $$
|
||||
|
||||
=== "Go"
|
||||
|
||||
```go title="time_complexity_types.go"
|
||||
```go title="time_complexity.go"
|
||||
/* 线性阶 */
|
||||
func linear(n int) int {
|
||||
count := 0
|
||||
@ -633,25 +633,25 @@ $$
|
||||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="time_complexity_types.js"
|
||||
```js title="time_complexity.js"
|
||||
|
||||
```
|
||||
|
||||
=== "TypeScript"
|
||||
|
||||
```typescript title="time_complexity_types.ts"
|
||||
```typescript title="time_complexity.ts"
|
||||
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="time_complexity_types.c"
|
||||
```c title="time_complexity.c"
|
||||
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="time_complexity_types.cs"
|
||||
```csharp title="time_complexity.cs"
|
||||
|
||||
```
|
||||
|
||||
@ -663,7 +663,7 @@ $$
|
||||
|
||||
=== "Java"
|
||||
|
||||
```java title="time_complexity_types.java"
|
||||
```java title="time_complexity.java"
|
||||
/* 线性阶(遍历数组) */
|
||||
int arrayTraversal(int[] nums) {
|
||||
int count = 0;
|
||||
@ -677,7 +677,7 @@ $$
|
||||
|
||||
=== "C++"
|
||||
|
||||
```cpp title="time_complexity_types.cpp"
|
||||
```cpp title="time_complexity.cpp"
|
||||
/* 线性阶(遍历数组) */
|
||||
int arrayTraversal(vector<int>& nums) {
|
||||
int count = 0;
|
||||
@ -691,7 +691,7 @@ $$
|
||||
|
||||
=== "Python"
|
||||
|
||||
```python title="time_complexity_types.py"
|
||||
```python title="time_complexity.py"
|
||||
""" 线性阶(遍历数组)"""
|
||||
def array_traversal(nums):
|
||||
count = 0
|
||||
@ -703,7 +703,7 @@ $$
|
||||
|
||||
=== "Go"
|
||||
|
||||
```go title="time_complexity_types.go"
|
||||
```go title="time_complexity.go"
|
||||
/* 线性阶(遍历数组) */
|
||||
func arrayTraversal(nums []int) int {
|
||||
count := 0
|
||||
@ -717,25 +717,25 @@ $$
|
||||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="time_complexity_types.js"
|
||||
```js title="time_complexity.js"
|
||||
|
||||
```
|
||||
|
||||
=== "TypeScript"
|
||||
|
||||
```typescript title="time_complexity_types.ts"
|
||||
```typescript title="time_complexity.ts"
|
||||
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="time_complexity_types.c"
|
||||
```c title="time_complexity.c"
|
||||
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="time_complexity_types.cs"
|
||||
```csharp title="time_complexity.cs"
|
||||
|
||||
```
|
||||
|
||||
@ -745,7 +745,7 @@ $$
|
||||
|
||||
=== "Java"
|
||||
|
||||
```java title="time_complexity_types.java"
|
||||
```java title="time_complexity.java"
|
||||
/* 平方阶 */
|
||||
int quadratic(int n) {
|
||||
int count = 0;
|
||||
@ -761,7 +761,7 @@ $$
|
||||
|
||||
=== "C++"
|
||||
|
||||
```cpp title="time_complexity_types.cpp"
|
||||
```cpp title="time_complexity.cpp"
|
||||
/* 平方阶 */
|
||||
int quadratic(int n) {
|
||||
int count = 0;
|
||||
@ -777,7 +777,7 @@ $$
|
||||
|
||||
=== "Python"
|
||||
|
||||
```python title="time_complexity_types.py"
|
||||
```python title="time_complexity.py"
|
||||
""" 平方阶 """
|
||||
def quadratic(n):
|
||||
count = 0
|
||||
@ -790,7 +790,7 @@ $$
|
||||
|
||||
=== "Go"
|
||||
|
||||
```go title="time_complexity_types.go"
|
||||
```go title="time_complexity.go"
|
||||
/* 平方阶 */
|
||||
func quadratic(n int) int {
|
||||
count := 0
|
||||
@ -806,25 +806,25 @@ $$
|
||||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="time_complexity_types.js"
|
||||
```js title="time_complexity.js"
|
||||
|
||||
```
|
||||
|
||||
=== "TypeScript"
|
||||
|
||||
```typescript title="time_complexity_types.ts"
|
||||
```typescript title="time_complexity.ts"
|
||||
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="time_complexity_types.c"
|
||||
```c title="time_complexity.c"
|
||||
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="time_complexity_types.cs"
|
||||
```csharp title="time_complexity.cs"
|
||||
|
||||
```
|
||||
|
||||
@ -840,7 +840,7 @@ $$
|
||||
|
||||
=== "Java"
|
||||
|
||||
```java title="time_complexity_types.java"
|
||||
```java title="time_complexity.java"
|
||||
/* 平方阶(冒泡排序) */
|
||||
int bubbleSort(int[] nums) {
|
||||
int count = 0; // 计数器
|
||||
@ -863,7 +863,7 @@ $$
|
||||
|
||||
=== "C++"
|
||||
|
||||
```cpp title="time_complexity_types.cpp"
|
||||
```cpp title="time_complexity.cpp"
|
||||
/* 平方阶(冒泡排序) */
|
||||
int bubbleSort(vector<int>& nums) {
|
||||
int count = 0; // 计数器
|
||||
@ -886,7 +886,7 @@ $$
|
||||
|
||||
=== "Python"
|
||||
|
||||
```python title="time_complexity_types.py"
|
||||
```python title="time_complexity.py"
|
||||
""" 平方阶(冒泡排序)"""
|
||||
def bubble_sort(nums):
|
||||
count = 0 # 计数器
|
||||
@ -905,7 +905,7 @@ $$
|
||||
|
||||
=== "Go"
|
||||
|
||||
```go title="time_complexity_types.go"
|
||||
```go title="time_complexity.go"
|
||||
/* 平方阶(冒泡排序) */
|
||||
func bubbleSort(nums []int) int {
|
||||
count := 0 // 计数器
|
||||
@ -928,25 +928,25 @@ $$
|
||||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="time_complexity_types.js"
|
||||
```js title="time_complexity.js"
|
||||
|
||||
```
|
||||
|
||||
=== "TypeScript"
|
||||
|
||||
```typescript title="time_complexity_types.ts"
|
||||
```typescript title="time_complexity.ts"
|
||||
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="time_complexity_types.c"
|
||||
```c title="time_complexity.c"
|
||||
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="time_complexity_types.cs"
|
||||
```csharp title="time_complexity.cs"
|
||||
|
||||
```
|
||||
|
||||
@ -960,7 +960,7 @@ $$
|
||||
|
||||
=== "Java"
|
||||
|
||||
```java title="time_complexity_types.java"
|
||||
```java title="time_complexity.java"
|
||||
/* 指数阶(循环实现) */
|
||||
int exponential(int n) {
|
||||
int count = 0, base = 1;
|
||||
@ -978,7 +978,7 @@ $$
|
||||
|
||||
=== "C++"
|
||||
|
||||
```cpp title="time_complexity_types.cpp"
|
||||
```cpp title="time_complexity.cpp"
|
||||
/* 指数阶(循环实现) */
|
||||
int exponential(int n) {
|
||||
int count = 0, base = 1;
|
||||
@ -996,7 +996,7 @@ $$
|
||||
|
||||
=== "Python"
|
||||
|
||||
```python title="time_complexity_types.py"
|
||||
```python title="time_complexity.py"
|
||||
""" 指数阶(循环实现)"""
|
||||
def exponential(n):
|
||||
count, base = 0, 1
|
||||
@ -1011,7 +1011,7 @@ $$
|
||||
|
||||
=== "Go"
|
||||
|
||||
```go title="time_complexity_types.go"
|
||||
```go title="time_complexity.go"
|
||||
/* 指数阶(循环实现)*/
|
||||
func exponential(n int) int {
|
||||
count, base := 0, 1
|
||||
@ -1029,25 +1029,25 @@ $$
|
||||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="time_complexity_types.js"
|
||||
```js title="time_complexity.js"
|
||||
|
||||
```
|
||||
|
||||
=== "TypeScript"
|
||||
|
||||
```typescript title="time_complexity_types.ts"
|
||||
```typescript title="time_complexity.ts"
|
||||
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="time_complexity_types.c"
|
||||
```c title="time_complexity.c"
|
||||
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="time_complexity_types.cs"
|
||||
```csharp title="time_complexity.cs"
|
||||
|
||||
```
|
||||
|
||||
@ -1059,7 +1059,7 @@ $$
|
||||
|
||||
=== "Java"
|
||||
|
||||
```java title="time_complexity_types.java"
|
||||
```java title="time_complexity.java"
|
||||
/* 指数阶(递归实现) */
|
||||
int expRecur(int n) {
|
||||
if (n == 1) return 1;
|
||||
@ -1069,7 +1069,7 @@ $$
|
||||
|
||||
=== "C++"
|
||||
|
||||
```cpp title="time_complexity_types.cpp"
|
||||
```cpp title="time_complexity.cpp"
|
||||
/* 指数阶(递归实现) */
|
||||
int expRecur(int n) {
|
||||
if (n == 1) return 1;
|
||||
@ -1079,7 +1079,7 @@ $$
|
||||
|
||||
=== "Python"
|
||||
|
||||
```python title="time_complexity_types.py"
|
||||
```python title="time_complexity.py"
|
||||
""" 指数阶(递归实现)"""
|
||||
def exp_recur(n):
|
||||
if n == 1: return 1
|
||||
@ -1088,7 +1088,7 @@ $$
|
||||
|
||||
=== "Go"
|
||||
|
||||
```go title="time_complexity_types.go"
|
||||
```go title="time_complexity.go"
|
||||
/* 指数阶(递归实现)*/
|
||||
func expRecur(n int) int {
|
||||
if n == 1 {
|
||||
@ -1100,25 +1100,25 @@ $$
|
||||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="time_complexity_types.js"
|
||||
```js title="time_complexity.js"
|
||||
|
||||
```
|
||||
|
||||
=== "TypeScript"
|
||||
|
||||
```typescript title="time_complexity_types.ts"
|
||||
```typescript title="time_complexity.ts"
|
||||
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="time_complexity_types.c"
|
||||
```c title="time_complexity.c"
|
||||
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="time_complexity_types.cs"
|
||||
```csharp title="time_complexity.cs"
|
||||
|
||||
```
|
||||
|
||||
@ -1132,7 +1132,7 @@ $$
|
||||
|
||||
=== "Java"
|
||||
|
||||
```java title="time_complexity_types.java"
|
||||
```java title="time_complexity.java"
|
||||
/* 对数阶(循环实现) */
|
||||
int logarithmic(float n) {
|
||||
int count = 0;
|
||||
@ -1146,7 +1146,7 @@ $$
|
||||
|
||||
=== "C++"
|
||||
|
||||
```cpp title="time_complexity_types.cpp"
|
||||
```cpp title="time_complexity.cpp"
|
||||
/* 对数阶(循环实现) */
|
||||
int logarithmic(float n) {
|
||||
int count = 0;
|
||||
@ -1160,7 +1160,7 @@ $$
|
||||
|
||||
=== "Python"
|
||||
|
||||
```python title="time_complexity_types.py"
|
||||
```python title="time_complexity.py"
|
||||
""" 对数阶(循环实现)"""
|
||||
def logarithmic(n):
|
||||
count = 0
|
||||
@ -1172,7 +1172,7 @@ $$
|
||||
|
||||
=== "Go"
|
||||
|
||||
```go title="time_complexity_types.go"
|
||||
```go title="time_complexity.go"
|
||||
/* 对数阶(循环实现)*/
|
||||
func logarithmic(n float64) int {
|
||||
count := 0
|
||||
@ -1186,25 +1186,25 @@ $$
|
||||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="time_complexity_types.js"
|
||||
```js title="time_complexity.js"
|
||||
|
||||
```
|
||||
|
||||
=== "TypeScript"
|
||||
|
||||
```typescript title="time_complexity_types.ts"
|
||||
```typescript title="time_complexity.ts"
|
||||
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="time_complexity_types.c"
|
||||
```c title="time_complexity.c"
|
||||
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="time_complexity_types.cs"
|
||||
```csharp title="time_complexity.cs"
|
||||
|
||||
```
|
||||
|
||||
@ -1216,7 +1216,7 @@ $$
|
||||
|
||||
=== "Java"
|
||||
|
||||
```java title="time_complexity_types.java"
|
||||
```java title="time_complexity.java"
|
||||
/* 对数阶(递归实现) */
|
||||
int logRecur(float n) {
|
||||
if (n <= 1) return 0;
|
||||
@ -1226,7 +1226,7 @@ $$
|
||||
|
||||
=== "C++"
|
||||
|
||||
```cpp title="time_complexity_types.cpp"
|
||||
```cpp title="time_complexity.cpp"
|
||||
/* 对数阶(递归实现) */
|
||||
int logRecur(float n) {
|
||||
if (n <= 1) return 0;
|
||||
@ -1236,7 +1236,7 @@ $$
|
||||
|
||||
=== "Python"
|
||||
|
||||
```python title="time_complexity_types.py"
|
||||
```python title="time_complexity.py"
|
||||
""" 对数阶(递归实现)"""
|
||||
def log_recur(n):
|
||||
if n <= 1: return 0
|
||||
@ -1245,7 +1245,7 @@ $$
|
||||
|
||||
=== "Go"
|
||||
|
||||
```go title="time_complexity_types.go"
|
||||
```go title="time_complexity.go"
|
||||
/* 对数阶(递归实现)*/
|
||||
func logRecur(n float64) int {
|
||||
if n <= 1 {
|
||||
@ -1257,25 +1257,25 @@ $$
|
||||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="time_complexity_types.js"
|
||||
```js title="time_complexity.js"
|
||||
|
||||
```
|
||||
|
||||
=== "TypeScript"
|
||||
|
||||
```typescript title="time_complexity_types.ts"
|
||||
```typescript title="time_complexity.ts"
|
||||
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="time_complexity_types.c"
|
||||
```c title="time_complexity.c"
|
||||
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="time_complexity_types.cs"
|
||||
```csharp title="time_complexity.cs"
|
||||
|
||||
```
|
||||
|
||||
@ -1287,7 +1287,7 @@ $$
|
||||
|
||||
=== "Java"
|
||||
|
||||
```java title="time_complexity_types.java"
|
||||
```java title="time_complexity.java"
|
||||
/* 线性对数阶 */
|
||||
int linearLogRecur(float n) {
|
||||
if (n <= 1) return 1;
|
||||
@ -1302,7 +1302,7 @@ $$
|
||||
|
||||
=== "C++"
|
||||
|
||||
```cpp title="time_complexity_types.cpp"
|
||||
```cpp title="time_complexity.cpp"
|
||||
/* 线性对数阶 */
|
||||
int linearLogRecur(float n) {
|
||||
if (n <= 1) return 1;
|
||||
@ -1317,7 +1317,7 @@ $$
|
||||
|
||||
=== "Python"
|
||||
|
||||
```python title="time_complexity_types.py"
|
||||
```python title="time_complexity.py"
|
||||
""" 线性对数阶 """
|
||||
def linear_log_recur(n):
|
||||
if n <= 1: return 1
|
||||
@ -1330,7 +1330,7 @@ $$
|
||||
|
||||
=== "Go"
|
||||
|
||||
```go title="time_complexity_types.go"
|
||||
```go title="time_complexity.go"
|
||||
/* 线性对数阶 */
|
||||
func linearLogRecur(n float64) int {
|
||||
if n <= 1 {
|
||||
@ -1347,25 +1347,25 @@ $$
|
||||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="time_complexity_types.js"
|
||||
```js title="time_complexity.js"
|
||||
|
||||
```
|
||||
|
||||
=== "TypeScript"
|
||||
|
||||
```typescript title="time_complexity_types.ts"
|
||||
```typescript title="time_complexity.ts"
|
||||
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="time_complexity_types.c"
|
||||
```c title="time_complexity.c"
|
||||
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="time_complexity_types.cs"
|
||||
```csharp title="time_complexity.cs"
|
||||
|
||||
```
|
||||
|
||||
@ -1385,7 +1385,7 @@ $$
|
||||
|
||||
=== "Java"
|
||||
|
||||
```java title="time_complexity_types.java"
|
||||
```java title="time_complexity.java"
|
||||
/* 阶乘阶(递归实现) */
|
||||
int factorialRecur(int n) {
|
||||
if (n == 0) return 1;
|
||||
@ -1400,7 +1400,7 @@ $$
|
||||
|
||||
=== "C++"
|
||||
|
||||
```cpp title="time_complexity_types.cpp"
|
||||
```cpp title="time_complexity.cpp"
|
||||
/* 阶乘阶(递归实现) */
|
||||
int factorialRecur(int n) {
|
||||
if (n == 0) return 1;
|
||||
@ -1415,7 +1415,7 @@ $$
|
||||
|
||||
=== "Python"
|
||||
|
||||
```python title="time_complexity_types.py"
|
||||
```python title="time_complexity.py"
|
||||
""" 阶乘阶(递归实现)"""
|
||||
def factorial_recur(n):
|
||||
if n == 0: return 1
|
||||
@ -1428,7 +1428,7 @@ $$
|
||||
|
||||
=== "Go"
|
||||
|
||||
```go title="time_complexity_types.go"
|
||||
```go title="time_complexity.go"
|
||||
/* 阶乘阶(递归实现) */
|
||||
func factorialRecur(n int) int {
|
||||
if n == 0 {
|
||||
@ -1445,25 +1445,25 @@ $$
|
||||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="time_complexity_types.js"
|
||||
```js title="time_complexity.js"
|
||||
|
||||
```
|
||||
|
||||
=== "TypeScript"
|
||||
|
||||
```typescript title="time_complexity_types.ts"
|
||||
```typescript title="time_complexity.ts"
|
||||
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="time_complexity_types.c"
|
||||
```c title="time_complexity.c"
|
||||
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="time_complexity_types.cs"
|
||||
```csharp title="time_complexity.cs"
|
||||
|
||||
```
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user