494. Target Sum

来源:互联网 发布:java patternlayout 编辑:程序博客网 时间:2024/06/07 07:20

You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have 2 symbols + and -. For each integer, you should choose one from + and - as its new symbol.

Find out how many ways to assign symbols to make sum of integers equal to target S.

Example 1:

Input: nums is [1, 1, 1, 1, 1], S is 3. Output: 5Explanation: -1+1+1+1+1 = 3+1-1+1+1+1 = 3+1+1-1+1+1 = 3+1+1+1-1+1 = 3+1+1+1+1-1 = 3There are 5 ways to assign symbols to make the sum of nums be target 3.

Note:

  1. The length of the given array is positive and will not exceed 20.
  2. The sum of elements in the given array will not exceed 1000.
  3. Your output answer is guaranteed to be fitted in a 32-bit integer.

把DFS,BFS,递归,DP的解法都写了一遍,本以为好流弊,但是发现Discuss里面还有更快的方法,可能是自己的代码还可以进一步优化(不需要每次都遍历从最小到最大)

package l494;import java.util.HashMap;import java.util.Map;/* * bfs */public class bfs {    public int findTargetSumWays(int[] nums, int S) {    Map<Integer, Integer> map = new HashMap<Integer, Integer>();    Map<Integer, Integer> map2 = new HashMap<Integer, Integer>();        int cnt = 1;    map.put(nums[0], 1);    map.put(-nums[0], 1);    if(nums[0] == 0)map.put(0, 2);        while(true) {    if(cnt == nums.length)break;    for(int i : map.keySet()) {    int add = i + nums[cnt];    if(map2.containsKey(add))        map2.put(add, map2.get(add)+map.get(i));        else        map2.put(add, map.get(i));        int minus = i - nums[cnt];        if(map2.containsKey(minus))        map2.put(minus, map2.get(minus)+map.get(i));        else        map2.put(minus, map.get(i));    }        map = map2;    map2 = new HashMap<Integer, Integer>();    cnt ++;    }        return map.containsKey(S) ? map.get(S) : 0;     }}


package l494;public class recrusive {    public int findTargetSumWays(int[] nums, int S) {        return findTargetSumWays(nums, 0, S);    }private int findTargetSumWays(int[] nums, int i, int s) {if(i == nums.length) {if(s == 0)return 1;return 0;}return findTargetSumWays(nums, i+1, s+nums[i]) + findTargetSumWays(nums, i+1, s-nums[i]);}}


package l494;public class recrusive2 {    public int findTargetSumWays(int[] nums, int S) {        return findTargetSumWays(nums, nums.length-1, S);    }private int findTargetSumWays(int[] nums, int i, int s) {if(i == -1) {if(s == 0)return 1;return 0;}return findTargetSumWays(nums, i-1, s+nums[i]) + findTargetSumWays(nums, i-1, s-nums[i]);}}


package l494;/* * 递归优化成DP * 因为有负数,所以要预处理 */public class Solution {    public int findTargetSumWays(int[] nums, int S) {    if(S > 1000)return 0;        int[][] dp = new int[nums.length][2004];    dp[0][nums[0]+1000] = 1;    dp[0][-nums[0]+1000] = 1;    if(nums[0] == 0)dp[0][1000] = 2;// 特殊情况        for(int i=1; i<nums.length; i++)     for(int j=0; j<2004; j++) {    if(j+nums[i] < 2004)    dp[i][j] += dp[i-1][j+nums[i]];    if(j-nums[i] >= 0)    dp[i][j] += dp[i-1][j-nums[i]];        }            return dp[nums.length-1][1000+S];    }}



package l494;/* * dfs */public class DFS {int rst = 0;    public int findTargetSumWays(int[] nums, int S) {    dfs(nums, 0, 0, S);    return rst;    }private void dfs(int[] nums, int i, int sum, int s) {if(i == nums.length) {if(sum == s)rst ++;return;}dfs(nums, i+1, sum+nums[i], s);dfs(nums, i+1, sum-nums[i], s);}}



0 0
原创粉丝点击