From 83a87ab5ad2f1dd0f81f3e779921a46c15d979f8 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Tue, 30 May 2023 16:28:09 +0800 Subject: [PATCH] =?UTF-8?q?Update=200279.=E5=AE=8C=E5=85=A8=E5=B9=B3?= =?UTF-8?q?=E6=96=B9=E6=95=B0.md=20about=20Rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0279.完全平方数.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/problems/0279.完全平方数.md b/problems/0279.完全平方数.md index f5b23d26..d5736951 100644 --- a/problems/0279.完全平方数.md +++ b/problems/0279.完全平方数.md @@ -345,6 +345,29 @@ function numSquares(n: number): number { }; ``` +Rust: + +```rust +// 先遍历背包 +impl Solution { + pub fn num_squares(n: i32) -> i32 { + let n = n as usize; + let mut dp = vec![i32::MAX; n + 1]; + dp[0] = 0; + for i in 0..=n { + let mut j = 1; + loop { + match j * j > i { + true => break, + false => dp[i] = dp[i].min(dp[i - j * j] + 1), + } + j += 1; + } + } + dp[n] + } +} +```