Last updated
Was this helpful?
Last updated
Was this helpful?
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.
整数数组中选一部分取负,其它不变,问一共有多少种取法能让最后的和等于S。把集合的数看成两个子集,满足sum(P) - sum(!P) = target 和sum(P) + sum(!P) = sum => sum(P) = (sum + target)/2,也就是找和为(sum+target)/2的子集数目,01背包。
https://leetcode.com/problems/target-sum/description/