mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
Merge pull request #2086 from fwqaaq/patch-43
Update 0494.目标和.md about rust
This commit is contained in:
@ -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">
|
<p align="center">
|
||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||||
|
Reference in New Issue
Block a user