rust and zig : add codes for chapter_dynamic_programming (#606)

* rust : add codes for chapter_dynamic_programming

* zig : add codes for chapter_dynamic_programming

* rust : add codes for chapter_backtracking

* Update n_queens.rs

---------

Co-authored-by: Yudong Jin <krahets@163.com>
This commit is contained in:
sjinzh
2023-07-15 23:16:02 +08:00
committed by GitHub
parent 5c09add1ec
commit b1f8857212
27 changed files with 1654 additions and 6 deletions
@@ -7,7 +7,7 @@
/* 爬楼梯:动态规划 */
fn climbing_stairs_dp(n: usize) -> i32 {
// 已知 dp[1] 和 dp[2] ,返回之
if n == 1 || n == 2 { return n as i32 };
if n == 1 || n == 2 { return n as i32; }
// 初始化 dp 表,用于存储子问题的解
let mut dp = vec![-1; n + 1];
// 初始状态:预设最小子问题的解
@@ -22,7 +22,7 @@ fn climbing_stairs_dp(n: usize) -> i32 {
/* 爬楼梯:状态压缩后的动态规划 */
fn climbing_stairs_dp_comp(n: usize) -> i32 {
if n == 1 || n == 2 { return n as i32 };
if n == 1 || n == 2 { return n as i32; }
let (mut a, mut b) = (1, 2);
for _ in 3..=n {
let tmp = b;