mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-13 18:00:18 +08:00
build
This commit is contained in:
@@ -206,22 +206,19 @@ comments: true
|
||||
|
||||
```csharp title=""
|
||||
/* 类 */
|
||||
class Node
|
||||
{
|
||||
class Node {
|
||||
int val;
|
||||
Node next;
|
||||
Node(int x) { val = x; }
|
||||
}
|
||||
|
||||
/* 函数 */
|
||||
int function()
|
||||
{
|
||||
int function() {
|
||||
// do something...
|
||||
return 0;
|
||||
}
|
||||
|
||||
int algorithm(int n) // 输入数据
|
||||
{
|
||||
int algorithm(int n) { // 输入数据
|
||||
const int a = 0; // 暂存数据(常量)
|
||||
int b = 0; // 暂存数据(变量)
|
||||
Node node = new Node(0); // 暂存数据(对象)
|
||||
@@ -323,7 +320,7 @@ comments: true
|
||||
=== "Python"
|
||||
|
||||
```python title=""
|
||||
def algorithm(n: int) -> None:
|
||||
def algorithm(n: int):
|
||||
a = 0 # O(1)
|
||||
b = [0] * 10000 # O(1)
|
||||
if n > 10:
|
||||
@@ -382,12 +379,10 @@ comments: true
|
||||
=== "C#"
|
||||
|
||||
```csharp title=""
|
||||
void algorithm(int n)
|
||||
{
|
||||
void algorithm(int n) {
|
||||
int a = 0; // O(1)
|
||||
int[] b = new int[10000]; // O(1)
|
||||
if (n > 10)
|
||||
{
|
||||
if (n > 10) {
|
||||
int[] nums = new int[n]; // O(n)
|
||||
}
|
||||
}
|
||||
@@ -472,7 +467,7 @@ comments: true
|
||||
# do something
|
||||
return 0
|
||||
|
||||
def loop(n: int) -> None:
|
||||
def loop(n: int):
|
||||
"""循环 O(1)"""
|
||||
for _ in range(n):
|
||||
function()
|
||||
@@ -570,22 +565,18 @@ comments: true
|
||||
=== "C#"
|
||||
|
||||
```csharp title=""
|
||||
int function()
|
||||
{
|
||||
int function() {
|
||||
// do something
|
||||
return 0;
|
||||
}
|
||||
/* 循环 O(1) */
|
||||
void loop(int n)
|
||||
{
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
void loop(int n) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
function();
|
||||
}
|
||||
}
|
||||
/* 递归 O(n) */
|
||||
int recur(int n)
|
||||
{
|
||||
int recur(int n) {
|
||||
if (n == 1) return 1;
|
||||
return recur(n - 1);
|
||||
}
|
||||
@@ -712,7 +703,7 @@ $$
|
||||
=== "Python"
|
||||
|
||||
```python title="space_complexity.py"
|
||||
def constant(n: int) -> None:
|
||||
def constant(n: int):
|
||||
"""常数阶"""
|
||||
# 常量、变量、对象占用 O(1) 空间
|
||||
a = 0
|
||||
@@ -951,7 +942,7 @@ $$
|
||||
=== "Python"
|
||||
|
||||
```python title="space_complexity.py"
|
||||
def linear(n: int) -> None:
|
||||
def linear(n: int):
|
||||
"""线性阶"""
|
||||
# 长度为 n 的列表占用 O(n) 空间
|
||||
nums = [0] * n
|
||||
@@ -1178,7 +1169,7 @@ $$
|
||||
=== "Python"
|
||||
|
||||
```python title="space_complexity.py"
|
||||
def linear_recur(n: int) -> None:
|
||||
def linear_recur(n: int):
|
||||
"""线性阶(递归实现)"""
|
||||
print("递归 n =", n)
|
||||
if n == 1:
|
||||
@@ -1326,7 +1317,7 @@ $$
|
||||
=== "Python"
|
||||
|
||||
```python title="space_complexity.py"
|
||||
def quadratic(n: int) -> None:
|
||||
def quadratic(n: int):
|
||||
"""平方阶"""
|
||||
# 二维列表占用 O(n^2) 空间
|
||||
num_matrix = [[0] * n for _ in range(n)]
|
||||
@@ -1568,8 +1559,9 @@ $$
|
||||
return 0;
|
||||
int *nums = malloc(sizeof(int) * n);
|
||||
printf("递归 n = %d 中的 nums 长度 = %d\r\n", n, n);
|
||||
int res = quadraticRecur(n - 1)
|
||||
free(nums);
|
||||
return quadraticRecur(n - 1);
|
||||
return res;
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
@@ -52,7 +52,7 @@ $$
|
||||
|
||||
```python title=""
|
||||
# 在某运行平台下
|
||||
def algorithm(n: int) -> None:
|
||||
def algorithm(n: int):
|
||||
a = 2 # 1 ns
|
||||
a = a + 1 # 1 ns
|
||||
a = a * 2 # 10 ns
|
||||
@@ -66,12 +66,12 @@ $$
|
||||
```go title=""
|
||||
// 在某运行平台下
|
||||
func algorithm(n int) {
|
||||
a := 2 // 1 ns
|
||||
a = a + 1 // 1 ns
|
||||
a = a * 2 // 10 ns
|
||||
a := 2 // 1 ns
|
||||
a = a + 1 // 1 ns
|
||||
a = a * 2 // 10 ns
|
||||
// 循环 n 次
|
||||
for i := 0; i < n; i++ { // 1 ns
|
||||
fmt.Println(a) // 5 ns
|
||||
for i := 0; i < n; i++ { // 1 ns
|
||||
fmt.Println(a) // 5 ns
|
||||
}
|
||||
}
|
||||
```
|
||||
@@ -125,15 +125,13 @@ $$
|
||||
|
||||
```csharp title=""
|
||||
// 在某运行平台下
|
||||
void algorithm(int n)
|
||||
{
|
||||
void algorithm(int n) {
|
||||
int a = 2; // 1 ns
|
||||
a = a + 1; // 1 ns
|
||||
a = a * 2; // 10 ns
|
||||
// 循环 n 次
|
||||
for (int i = 0; i < n; i++)
|
||||
{ // 1 ns ,每轮都要执行 i++
|
||||
Console.WriteLine(0); // 5 ns
|
||||
for (int i = 0; i < n; i++) { // 1 ns ,每轮都要执行 i++
|
||||
Console.WriteLine(0); // 5 ns
|
||||
}
|
||||
}
|
||||
```
|
||||
@@ -232,14 +230,14 @@ $$
|
||||
|
||||
```python title=""
|
||||
# 算法 A 时间复杂度:常数阶
|
||||
def algorithm_A(n: int) -> None:
|
||||
def algorithm_A(n: int):
|
||||
print(0)
|
||||
# 算法 B 时间复杂度:线性阶
|
||||
def algorithm_B(n: int) -> None:
|
||||
def algorithm_B(n: int):
|
||||
for _ in range(n):
|
||||
print(0)
|
||||
# 算法 C 时间复杂度:常数阶
|
||||
def algorithm_C(n: int) -> None:
|
||||
def algorithm_C(n: int):
|
||||
for _ in range(1000000):
|
||||
print(0)
|
||||
```
|
||||
@@ -333,23 +331,18 @@ $$
|
||||
|
||||
```csharp title=""
|
||||
// 算法 A 时间复杂度:常数阶
|
||||
void algorithm_A(int n)
|
||||
{
|
||||
void algorithm_A(int n) {
|
||||
Console.WriteLine(0);
|
||||
}
|
||||
// 算法 B 时间复杂度:线性阶
|
||||
void algorithm_B(int n)
|
||||
{
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
void algorithm_B(int n) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
Console.WriteLine(0);
|
||||
}
|
||||
}
|
||||
// 算法 C 时间复杂度:常数阶
|
||||
void algorithm_C(int n)
|
||||
{
|
||||
for (int i = 0; i < 1000000; i++)
|
||||
{
|
||||
void algorithm_C(int n) {
|
||||
for (int i = 0; i < 1000000; i++) {
|
||||
Console.WriteLine(0);
|
||||
}
|
||||
}
|
||||
@@ -456,7 +449,7 @@ $$
|
||||
=== "Python"
|
||||
|
||||
```python title=""
|
||||
def algorithm(n: int) -> None:
|
||||
def algorithm(n: int):
|
||||
a = 1 # +1
|
||||
a = a + 1 # +1
|
||||
a = a * 2 # +1
|
||||
@@ -524,14 +517,12 @@ $$
|
||||
=== "C#"
|
||||
|
||||
```csharp title=""
|
||||
void algorithm(int n)
|
||||
{
|
||||
void algorithm(int n) {
|
||||
int a = 1; // +1
|
||||
a = a + 1; // +1
|
||||
a = a * 2; // +1
|
||||
// 循环 n 次
|
||||
for (int i = 0; i < n; i++) // +1(每轮都执行 i ++)
|
||||
{
|
||||
for (int i = 0; i < n; i++) { // +1(每轮都执行 i ++)
|
||||
Console.WriteLine(0); // +1
|
||||
}
|
||||
}
|
||||
@@ -661,7 +652,7 @@ $$
|
||||
=== "Python"
|
||||
|
||||
```python title=""
|
||||
def algorithm(n: int) -> None:
|
||||
def algorithm(n: int):
|
||||
a = 1 # +0(技巧 1)
|
||||
a = a + n # +0(技巧 1)
|
||||
# +n(技巧 2)
|
||||
@@ -752,20 +743,16 @@ $$
|
||||
=== "C#"
|
||||
|
||||
```csharp title=""
|
||||
void algorithm(int n)
|
||||
{
|
||||
void algorithm(int n) {
|
||||
int a = 1; // +0(技巧 1)
|
||||
a = a + n; // +0(技巧 1)
|
||||
// +n(技巧 2)
|
||||
for (int i = 0; i < 5 * n + 1; i++)
|
||||
{
|
||||
for (int i = 0; i < 5 * n + 1; i++) {
|
||||
Console.WriteLine(0);
|
||||
}
|
||||
// +n*n(技巧 3)
|
||||
for (int i = 0; i < 2 * n; i++)
|
||||
{
|
||||
for (int j = 0; j < n + 1; j++)
|
||||
{
|
||||
for (int i = 0; i < 2 * n; i++) {
|
||||
for (int j = 0; j < n + 1; j++) {
|
||||
Console.WriteLine(0);
|
||||
}
|
||||
}
|
||||
@@ -1662,9 +1649,7 @@ $$
|
||||
for (int j = 0; j < i; j++) {
|
||||
if (nums[j] > nums[j + 1]) {
|
||||
// 交换 nums[j] 与 nums[j + 1]
|
||||
int tmp = nums[j];
|
||||
nums[j] = nums[j + 1];
|
||||
nums[j + 1] = tmp;
|
||||
(nums[j + 1], nums[j]) = (nums[j], nums[j + 1]);
|
||||
count += 3; // 元素交换包含 3 个单元操作
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user