题目
想不到dp方程怎么写 写一发搜索试试 还好过了
public class LeetCode494 {
int res = 0;
public int findTargetSumWays(int[] nums, int S) {
dfs(nums,S,0,0);
return res;
}
private void dfs(int[] nums, int k, int sum, int step){
if (step == nums.length) {
if (sum == k) res++;
return;
}
dfs(nums,k,sum+nums[step],step+1);
dfs(nums,k,sum-nums[step],step+1);
}
}