494. Target Sum Medium

@problem@discussion
#Array#Dynamic Programming#Backtracking



1/**
2 * [494] Target Sum
3 *
4 * You are given an integer array nums and an integer target.
5 * You want to build an expression out of nums by adding one of the symbols '+' and '-' before each integer in nums and then concatenate all the integers.
6 * 
7 * 	For example, if nums = [2, 1], you can add a '+' before 2 and a '-' before 1 and concatenate them to build the expression "+2-1".
8 * 
9 * Return the number of different expressions that you can build, which evaluates to target.
10 *  
11 * Example 1:
12 * 
13 * Input: nums = [1,1,1,1,1], target = 3
14 * Output: 5
15 * Explanation: There are 5 ways to assign symbols to make the sum of nums be target 3.
16 * -1 + 1 + 1 + 1 + 1 = 3
17 * +1 - 1 + 1 + 1 + 1 = 3
18 * +1 + 1 - 1 + 1 + 1 = 3
19 * +1 + 1 + 1 - 1 + 1 = 3
20 * +1 + 1 + 1 + 1 - 1 = 3
21 * 
22 * Example 2:
23 * 
24 * Input: nums = [1], target = 1
25 * Output: 1
26 * 
27 *  
28 * Constraints:
29 * 
30 * 	1 <= nums.length <= 20
31 * 	0 <= nums[i] <= 1000
32 * 	0 <= sum(nums[i]) <= 1000
33 * 	-1000 <= target <= 1000
34 * 
35 */
36pub struct Solution {}
37
38// problem: https://leetcode.com/problems/target-sum/
39// discuss: https://leetcode.com/problems/target-sum/discuss/?currentPage=1&orderBy=most_votes&query=
40
41// submission codes start here
42
43impl Solution {
44    pub fn find_target_sum_ways(nums: Vec<i32>, target: i32) -> i32 {
45        0
46    }
47}
48
49// submission codes end
50
51#[cfg(test)]
52mod tests {
53    use super::*;
54
55    #[test]
56    fn test_494() {
57    }
58}
59


Back
© 2025 bowen.ge All Rights Reserved.