From 60f3ad665b005e518a6b413304bfbf26058feeb5 Mon Sep 17 00:00:00 2001 From: cezarbbb <105843128+cezarbbb@users.noreply.github.com> Date: Sun, 7 Aug 2022 19:34:04 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200343.=E6=95=B4=E6=95=B0?= =?UTF-8?q?=E6=8B=86=E5=88=86=20Rust=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 0343.整数拆分 Rust版本 --- problems/0343.整数拆分.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/problems/0343.整数拆分.md b/problems/0343.整数拆分.md index a4d532fd..60676e85 100644 --- a/problems/0343.整数拆分.md +++ b/problems/0343.整数拆分.md @@ -299,6 +299,27 @@ function integerBreak(n: number): number { }; ``` +### Rust + +```Rust +impl Solution { + fn max(a: i32, b: i32) -> i32{ + if a > b { a } else { b } + } + pub fn integer_break(n: i32) -> i32 { + let n = n as usize; + let mut dp = vec![0; n + 1]; + dp[2] = 1; + for i in 3..=n { + for j in 1..i - 1 { + dp[i] = Self::max(dp[i], Self::max(((i - j) * j) as i32, dp[i - j] * j as i32)); + } + } + dp[n] + } +} +``` + ### C ```c