LeetCode 494. Target Sum

Problem Statement


impl Solution {
fn dfs(nums: &Vec<i32>, index: i32, sum: i32, s: i32, ans: &mut i32) {
if index == nums.len() as i32 {
if sum == s {
*ans += 1;
}
return;
}

Self::dfs(nums, index + 1, sum + nums[index as usize], s, ans);
Self::dfs(nums, index + 1, sum - nums[index as usize], s, ans);
}
pub fn find_target_sum_ways(nums: Vec<i32>, s: i32) -> i32 {
let mut ans = 0;
Self::dfs(&nums, 0, 0, s, &mut ans);
return ans;
}
}