← 返回 meta 的题目列表Target Sum Expression
类型:online_judge
Given a list of numbers and a target value, insert + or - operators or choose not to insert any operators between numbers to calculate the target value. Find out all possible combinations.
Input
A list of integers nums with length n, where 1 <= n <= 20.
A target integer target, where -1000 <= target <= 1000.
Output
The number of possible combinations to reach the target value.
Example 1
Input: nums = [1, 1, 1, 1, 1], target = 3
Output: 5
Explanation: There are 5 ways to get 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
- +1+1+1+1-1=3
Example 2
Input: nums = [1], target = 1
Output: 1
Note
Each element is treated independently and can be followed by a + or - sign, or no sign at all. Find all possible combinations that lead to the target value.
Example
Input
5
1 1 1 1 1
3