From 872402e5a4e2e227e889fd3103786c503f864abd Mon Sep 17 00:00:00 2001 From: fw_qaq Date: Thu, 13 Apr 2023 18:05:44 +0800 Subject: [PATCH] =?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