This commit is contained in:
krahets
2024-03-18 03:11:07 +08:00
parent bc0054a577
commit 54c7448946
48 changed files with 577 additions and 408 deletions

View File

@ -979,7 +979,7 @@ Note that memory occupied by initializing variables or calling functions in a lo
```rust title="space_complexity.rs"
/* 函数 */
fn function() ->i32 {
fn function() -> i32 {
// 执行某些操作
return 0;
}
@ -1452,7 +1452,9 @@ As shown below, this function's recursive depth is $n$, meaning there are $n$ in
/* 线性阶(递归实现) */
fn linear_recur(n: i32) {
println!("递归 n = {}", n);
if n == 1 {return};
if n == 1 {
return;
};
linear_recur(n - 1);
}
```
@ -1834,7 +1836,9 @@ As shown below, the recursive depth of this function is $n$, and in each recursi
```rust title="space_complexity.rs"
/* 平方阶(递归实现) */
fn quadratic_recur(n: i32) -> i32 {
if n <= 0 {return 0};
if n <= 0 {
return 0;
};
// 数组 nums 长度为 n, n-1, ..., 2, 1
let nums = vec![0; n as usize];
println!("递归 n = {} 中的 nums 长度 = {}", n, nums.len());
@ -2011,7 +2015,9 @@ Exponential order is common in binary trees. Observe the below image, a "full bi
```rust title="space_complexity.rs"
/* 指数阶(建立满二叉树) */
fn build_tree(n: i32) -> Option<Rc<RefCell<TreeNode>>> {
if n == 0 {return None};
if n == 0 {
return None;
};
let root = TreeNode::new(0);
root.borrow_mut().left = build_tree(n - 1);
root.borrow_mut().right = build_tree(n - 1);