From 872402e5a4e2e227e889fd3103786c503f864abd Mon Sep 17 00:00:00 2001 From: fw_qaq Date: Thu, 13 Apr 2023 18:05:44 +0800 Subject: [PATCH 1/2] =?UTF-8?q?Update=200063.=E4=B8=8D=E5=90=8C=E8=B7=AF?= =?UTF-8?q?=E5=BE=84II.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0063.不同路径II.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/problems/0063.不同路径II.md b/problems/0063.不同路径II.md index 85130ab4..8ab90e6d 100644 --- a/problems/0063.不同路径II.md +++ b/problems/0063.不同路径II.md @@ -493,6 +493,34 @@ impl Solution { } ``` +空间优化: + +```rust +impl Solution { + pub fn unique_paths_with_obstacles(obstacle_grid: Vec>) -> i32 { + let mut dp = vec![0; obstacle_grid[0].len()]; + for (i, &v) in obstacle_grid[0].iter().enumerate() { + if v == 0 { + dp[i] = 1; + } else { + break; + } + } + for rows in obstacle_grid.iter().skip(1) { + for j in 0..rows.len() { + if rows[j] == 1 { + dp[j] = 0; + continue; + } else if j != 0 { + dp[j] += dp[j - 1]; + } + } + } + dp.pop().unwrap() + } +} +``` + ### C ```c From ced650c7bb9d43447e8445fff31469455440057e Mon Sep 17 00:00:00 2001 From: fw_qaq Date: Thu, 13 Apr 2023 18:06:59 +0800 Subject: [PATCH 2/2] =?UTF-8?q?Update=20problems/0063.=E4=B8=8D=E5=90=8C?= =?UTF-8?q?=E8=B7=AF=E5=BE=84II.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0063.不同路径II.md | 1 - 1 file changed, 1 deletion(-) diff --git a/problems/0063.不同路径II.md b/problems/0063.不同路径II.md index 8ab90e6d..8ff8c33f 100644 --- a/problems/0063.不同路径II.md +++ b/problems/0063.不同路径II.md @@ -510,7 +510,6 @@ impl Solution { for j in 0..rows.len() { if rows[j] == 1 { dp[j] = 0; - continue; } else if j != 0 { dp[j] += dp[j - 1]; }