1
impl Solution {
2
pub fn find_target_sum_ways(nums: Vec<i32>, target: i32) -> i32 {
3
let mut count = 0;
4
Self::dfs(&nums, target, 0, 0, &mut count);
5
count
6
}
7
fn dfs(nums: &Vec<i32>, target: i32, current_sum: i32, current_length: usize, res: &mut i32) {
8
if current_length == nums.len() {
9
if current_sum == target {
10
*res += 1;
11
}
12
return;
13
}
14
let curr_num = nums[current_length];
15
Self::dfs(
16
nums,
17
target,
18
current_sum + curr_num,
19
current_length + 1,
20
res,
21
);
22
Self::dfs(
23
nums,
24
target,
25
current_sum - curr_num,
26
current_length + 1,
27
res,
28
);
29
}
30
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0