mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-25 12:36:30 +08:00
feat: add rust codes for space_complexity (#409)
This commit is contained in:
parent
82bbdd444d
commit
2029d2b939
3 changed files with 117 additions and 2 deletions
|
@ -14,6 +14,11 @@ path = "chapter_computational_complexity/time_complexity.rs"
|
|||
name = "worst_best_time_complexity"
|
||||
path = "chapter_computational_complexity/worst_best_time_complexity.rs"
|
||||
|
||||
# Run Command: cargo run --bin space_complexity
|
||||
[[bin]]
|
||||
name = "space_complexity"
|
||||
path = "chapter_computational_complexity/space_complexity.rs"
|
||||
|
||||
# Run Command: cargo run --bin leetcode_two_sum
|
||||
[[bin]]
|
||||
name = "leetcode_two_sum"
|
||||
|
|
109
codes/rust/chapter_computational_complexity/space_complexity.rs
Normal file
109
codes/rust/chapter_computational_complexity/space_complexity.rs
Normal file
|
@ -0,0 +1,109 @@
|
|||
/*
|
||||
* File: space_complexity.rs
|
||||
* Created Time: 2023-03-11
|
||||
* Author: sjinzh (sjinzh@gmail.com)
|
||||
*/
|
||||
|
||||
include!("../include/include.rs");
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::rc::Rc;
|
||||
use std::cell::RefCell;
|
||||
|
||||
/* 函数 */
|
||||
fn function() ->i32 {
|
||||
// do something
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* 常数阶 */
|
||||
#[allow(unused)]
|
||||
fn constant(n: i32) {
|
||||
// 常量、变量、对象占用 O(1) 空间
|
||||
const A: i32 = 0;
|
||||
let b = 0;
|
||||
let nums = vec![0; 10000];
|
||||
let node = ListNode::new(0);
|
||||
// 循环中的变量占用 O(1) 空间
|
||||
for i in 0..n {
|
||||
let c = 0;
|
||||
}
|
||||
// 循环中的函数占用 O(1) 空间
|
||||
for i in 0..n {
|
||||
function();
|
||||
}
|
||||
}
|
||||
|
||||
/* 线性阶 */
|
||||
#[allow(unused)]
|
||||
fn linear(n: i32) {
|
||||
// 长度为 n 的数组占用 O(n) 空间
|
||||
let mut nums = vec![0; n as usize];
|
||||
// 长度为 n 的列表占用 O(n) 空间
|
||||
let mut nodes = Vec::new();
|
||||
for i in 0..n {
|
||||
nodes.push(ListNode::new(i))
|
||||
}
|
||||
// 长度为 n 的哈希表占用 O(n) 空间
|
||||
let mut map = HashMap::new();
|
||||
for i in 0..n {
|
||||
map.insert(i, i.to_string());
|
||||
}
|
||||
}
|
||||
|
||||
/* 线性阶(递归实现) */
|
||||
fn linear_recur(n: i32) {
|
||||
println!("递归 n = {}", n);
|
||||
if n == 1 {return};
|
||||
linear_recur(n - 1);
|
||||
}
|
||||
|
||||
/* 平方阶 */
|
||||
#[allow(unused)]
|
||||
fn quadratic(n: i32) {
|
||||
// 矩阵占用 O(n^2) 空间
|
||||
let num_matrix = vec![vec![0; n as usize]; n as usize];
|
||||
// 二维列表占用 O(n^2) 空间
|
||||
let mut num_list = Vec::new();
|
||||
for i in 0..n {
|
||||
let mut tmp = Vec::new();
|
||||
for j in 0..n {
|
||||
tmp.push(0);
|
||||
}
|
||||
num_list.push(tmp);
|
||||
}
|
||||
}
|
||||
|
||||
/* 平方阶(递归实现) */
|
||||
fn quadratic_recur(n: i32) -> i32 {
|
||||
if n <= 0 {return 0};
|
||||
// 数组 nums 长度为 n, n-1, ..., 2, 1
|
||||
let nums = vec![0; n as usize];
|
||||
println!("递归 n = {} 中的 nums 长度 = {}", n, nums.len());
|
||||
return quadratic_recur(n - 1);
|
||||
}
|
||||
|
||||
/* 指数阶(建立满二叉树) */
|
||||
fn build_tree(n: i32) -> Option<Rc<RefCell<TreeNode>>> {
|
||||
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);
|
||||
return Some(root);
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
fn main() {
|
||||
let n = 5;
|
||||
// 常数阶
|
||||
constant(n);
|
||||
// 线性阶
|
||||
linear(n);
|
||||
linear_recur(n);
|
||||
// 平方阶
|
||||
quadratic(n);
|
||||
quadratic_recur(n);
|
||||
// 指数阶
|
||||
let root = build_tree(n);
|
||||
print_util::print_tree(&root.unwrap());
|
||||
}
|
|
@ -5,6 +5,7 @@
|
|||
*/
|
||||
|
||||
pub mod print_util;
|
||||
pub mod tree_node;
|
||||
pub mod list_node;
|
||||
pub use list_node::ListNode;
|
||||
pub mod tree_node;
|
||||
pub use tree_node::TreeNode;
|
Loading…
Reference in a new issue