mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-16 05:30:59 +00:00
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:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user