Merge pull request #2104 from fwqaaq/patch-25

Update 0377.组合总和Ⅳ.md 优化 rust
This commit is contained in:
程序员Carl 2023-06-09 07:34:07 +08:00 committed by GitHub
commit cf8e53a94c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 7 additions and 6 deletions

View File

@ -269,16 +269,17 @@ Rust
```Rust
impl Solution {
pub fn combination_sum4(nums: Vec<i32>, target: i32) -> i32 {
let mut dp = vec![0; target as usize + 1];
let target = target as usize;
let mut dp = vec![0; target + 1];
dp[0] = 1;
for i in 1..=target as usize {
for &j in nums.iter() {
if i as i32 >= j {
dp[i] += dp[i- j as usize];
for i in 1..=target {
for &n in &nums {
if i >= n as usize {
dp[i] += dp[i - n as usize];
}
}
}
return dp[target as usize];
dp[target]
}
}
```