Skip to content

Commit

Permalink
Merge pull request youngyangyang04#2086 from fwqaaq/patch-43
Browse files Browse the repository at this point in the history
Update 0494.目标和.md about rust
  • Loading branch information
youngyangyang04 authored Jun 1, 2023
2 parents bf2d349 + 03829cd commit 6156804
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions problems/0494.目标和.md
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,31 @@ object Solution {
}
```

### Rust

```rust
impl Solution {
pub fn find_target_sum_ways(nums: Vec<i32>, target: i32) -> i32 {
let sum = nums.iter().sum::<i32>();
if target.abs() > sum {
return 0;
}
if (target + sum) % 2 == 1 {
return 0;
}
let size = (sum + target) as usize / 2;
let mut dp = vec![0; size + 1];
dp[0] = 1;
for n in nums {
for s in (n as usize..=size).rev() {
dp[s] += dp[s - n as usize];
}
}
dp[size]
}
}
```

<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
Expand Down

0 comments on commit 6156804

Please sign in to comment.