Leetcode494-target Sum-medium

Source: Internet
Author: User

You are given a list of non-negative integers, a1, A2, ..., an, and a target, S. Now there are 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:5
Explanation:
-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 = 3
There is 5 ways to assign symbols to make the sum of Nums is target 3.
Note:
1. The length of the given array is positive and would not be exceed 20.
2. The sum of elements in the given array would not exceed 1000.
3. Your output answer is guaranteed to being fitted in a 32-bit integer.

1.DFS. O (2^n)
Try all +-+ combinations.

2.DP


There is a limit on the subject, that is, the final sum in 1000, in fact, they add and subtract the answer of the kind not so much. So if you've been using only a map to figure out how many ways the current number is calculated, you'll be able to iterate over the map from the previous round map by the time you encounter a new number.
DP[I][J] represents the number from the 0~i-1, spelling out how many spellings of J, the final answer is Dp[last][target]

3. HashMap of class DP.
Save space compared to map, just remember the sum object that is currently exactly present. The possibility of getting a new map based on the old map each time.

Implementing 1 DFS:

classSolution { Public intFindtargetsumways (int[] Nums,intS) {if(Nums = =NULL) {            return0; }        returnDFS (nums, 0, S, 0); }        Private intDfsint[] Nums,intCrtintTargetintoffset) {        if(offset = =nums.length) {returnCRT = = target? 1:0; }        intAns = 0; Ans+ = DFS (nums, CRT + Nums[offset], target, offset + 1); Ans+ = DFS (nums, Crt-nums[offset], target, offset + 1); returnans; }}

Achieve 3 HashMap:

classSolution { Public intFindtargetsumways (int[] Nums,intS) {Map<integer, integer> count =NewHashmap<>(); Count.put (0, 1);  for(intnum:nums) {Map<integer, integer> newcount =NewHashmap<>();  for(intSum:count.keySet ()) {Newcount.put (sum+ num, newcount.getordefault (sum + num, 0) +count.get (sum)); Newcount.put (Sum-Num, Newcount.getordefault (sum-num, 0) +count.get (sum)); } Count=Newcount; }        returnCount.getordefault (S, 0); }}

Leetcode494-target Sum-medium

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.