L494
//方法一:回溯法
class Solution {
public static int findTargetSumWays(int[] nums, int target) {
cnt =0;
recurve(nums, 0, 0, target);
return cnt;
}
static int cnt = 0;
public static void recurve(int[] nums, int n, int sum, int target) {
if (n == nums.length) {
if (sum == target) cnt++;
return;
}
recurve(nums, n + 1, sum + nums[n], target);
recurve(nums, n + 1, sum - nums[n], target);
}
}
//方法二:深度优先搜索