mirror of
https://github.com/krahets/hello-algo.git
synced 2025-07-04 20:31:59 +08:00
feat: modify some Dart codes and add Dart code blocks to the docs (#543)
This commit is contained in:
@ -261,7 +261,26 @@
|
||||
=== "Dart"
|
||||
|
||||
```dart title=""
|
||||
/* 类 */
|
||||
class Node {
|
||||
int val;
|
||||
Node next;
|
||||
Node(this.val, [this.next]);
|
||||
}
|
||||
|
||||
/* 函数 */
|
||||
int function() {
|
||||
// do something...
|
||||
return 0;
|
||||
}
|
||||
|
||||
int algorithm(int n) { // 输入数据
|
||||
const int a = 0; // 暂存数据(常量)
|
||||
int b = 0; // 暂存数据(变量)
|
||||
Node node = Node(0); // 暂存数据(对象)
|
||||
int c = function(); // 栈帧空间(调用函数)
|
||||
return a + b + c; // 输出数据
|
||||
}
|
||||
```
|
||||
|
||||
## 推算方法
|
||||
@ -389,7 +408,13 @@
|
||||
=== "Dart"
|
||||
|
||||
```dart title=""
|
||||
|
||||
void algorithm(int n) {
|
||||
int a = 0; // O(1)
|
||||
List<int> b = List.filled(10000, 0); // O(1)
|
||||
if (n > 10) {
|
||||
List<int> nums = List.filled(n, 0); // O(n)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**在递归函数中,需要注意统计栈帧空间**。例如,函数 `loop()` 在循环中调用了 $n$ 次 `function()` ,每轮中的 `function()` 都返回并释放了栈帧空间,因此空间复杂度仍为 $O(1)$ 。而递归函数 `recur()` 在运行过程中会同时存在 $n$ 个未返回的 `recur()` ,从而占用 $O(n)$ 的栈帧空间。
|
||||
@ -594,7 +619,21 @@
|
||||
=== "Dart"
|
||||
|
||||
```dart title=""
|
||||
|
||||
int function() {
|
||||
// do something
|
||||
return 0;
|
||||
}
|
||||
/* 循环 O(1) */
|
||||
void loop(int n) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
function();
|
||||
}
|
||||
}
|
||||
/* 递归 O(n) */
|
||||
void recur(int n) {
|
||||
if (n == 1) return;
|
||||
return recur(n - 1);
|
||||
}
|
||||
```
|
||||
|
||||
## 常见类型
|
||||
|
@ -158,7 +158,16 @@ $$
|
||||
=== "Dart"
|
||||
|
||||
```dart title=""
|
||||
|
||||
// 在某运行平台下
|
||||
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++
|
||||
print(0); // 5 ns
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
然而实际上,**统计算法的运行时间既不合理也不现实**。首先,我们不希望预估时间和运行平台绑定,因为算法需要在各种不同的平台上运行。其次,我们很难获知每种操作的运行时间,这给预估过程带来了极大的难度。
|
||||
@ -374,7 +383,22 @@ $$
|
||||
=== "Dart"
|
||||
|
||||
```dart title=""
|
||||
|
||||
// 算法 A 时间复杂度:常数阶
|
||||
void algorithmA(int n) {
|
||||
print(0);
|
||||
}
|
||||
// 算法 B 时间复杂度:线性阶
|
||||
void algorithmB(int n) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
print(0);
|
||||
}
|
||||
}
|
||||
// 算法 C 时间复杂度:常数阶
|
||||
void algorithmC(int n) {
|
||||
for (int i = 0; i < 1000000; i++) {
|
||||
print(0);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||

|
||||
@ -530,7 +554,15 @@ $$
|
||||
=== "Dart"
|
||||
|
||||
```dart title=""
|
||||
|
||||
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 ++)
|
||||
print(0); // +1
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
$T(n)$ 是一次函数,说明时间增长趋势是线性的,因此可以得出时间复杂度是线性阶。
|
||||
@ -760,7 +792,20 @@ $$
|
||||
=== "Dart"
|
||||
|
||||
```dart title=""
|
||||
|
||||
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++) {
|
||||
print(0);
|
||||
}
|
||||
// +n*n(技巧 3)
|
||||
for (int i = 0; i < 2 * n; i++) {
|
||||
for (int j = 0; j < n + 1; j++) {
|
||||
print(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 2) 判断渐近上界
|
||||
|
Reference in New Issue
Block a user