[LeetCode] 494. Target Sum 目标和
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: 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 are 5 ways to assign symbols to make the sum of nums be target 3.
Note:
- The length of the given array is positive and will not exceed 20.
- The sum of elements in the given array will not exceed 1000.
- Your output answer is guaranteed to be fitted in a 32-bit integer.
给一个由非负整数组成的数组,和一个目标值,给数字前面加上正号或负号,然后求和,问和目标值相等的情况有多少。
解法:递归,循环数组里的数字,调用递归函数,分别对目标值进行加上和减去当前数字,再调用递归,这样就会涵盖所有情况,若目标值为0了,则结果res自增1。
Java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | public int findTargetSumWays( int [] nums, int s) { int sum = 0 ; for ( int n : nums) sum += n; return sum < s || (s + sum) % 2 > 0 ? 0 : subsetSum(nums, (s + sum) >>> 1 ); } public int subsetSum( int [] nums, int s) { int [] dp = new int [s + 1 ]; dp[ 0 ] = 1 ; for ( int n : nums) for ( int i = s; i >= n; i--) dp[i] += dp[i - n]; return dp[s]; } |
Python:
1 2 3 4 5 6 7 8 9 10 11 12 | class Solution( object ): def findTargetSumWays( self , nums, S): if not nums: return 0 dic = {nums[ 0 ]: 1 , - nums[ 0 ]: 1 } if nums[ 0 ] ! = 0 else { 0 : 2 } for i in range ( 1 , len (nums)): tdic = {} for d in dic: tdic[d + nums[i]] = tdic.get(d + nums[i], 0 ) + dic.get(d, 0 ) tdic[d - nums[i]] = tdic.get(d - nums[i], 0 ) + dic.get(d, 0 ) dic = tdic return dic.get(S, 0 ) |
C++:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | class Solution { public : int findTargetSumWays(vector< int >& nums, int s) { int sum = accumulate(nums.begin(), nums.end(), 0); return sum < s || (s + sum) & 1 ? 0 : subsetSum(nums, (s + sum) >> 1); } int subsetSum(vector< int >& nums, int s) { int dp[s + 1] = { 0 }; dp[0] = 1; for ( int n : nums) for ( int i = s; i >= n; i--) dp[i] += dp[i - n]; return dp[s]; } }; |
C++:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | class Solution { public : int findTargetSumWays(vector< int >& nums, int S) { int res = 0; helper(nums, S, 0, res); return res; } void helper(vector< int >& nums, int S, int start, int & res) { if (start >= nums.size()) { if (S == 0) ++res; return ; } helper(nums, S - nums[start], start + 1, res); helper(nums, S + nums[start], start + 1, res); } }; |
C++: 使用dp记录中间值优化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | class Solution { public : int findTargetSumWays(vector< int >& nums, int S) { vector<unordered_map< int , int >> dp(nums.size()); return helper(nums, S, 0, dp); } int helper(vector< int >& nums, int sum, int start, vector<unordered_map< int , int >>& dp) { if (start == nums.size()) return sum == 0; if (dp[start].count(sum)) return dp[start][sum]; int cnt1 = helper(nums, sum - nums[start], start + 1, dp); int cnt2 = helper(nums, sum + nums[start], start + 1, dp); return dp[start][sum] = cnt1 + cnt2; } }; |
C++:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | class Solution { public : int findTargetSumWays(vector< int >& nums, int S) { int n = nums.size(); vector<unordered_map< int , int >> dp(n + 1); dp[0][0] = 1; for ( int i = 0; i < n; ++i) { for ( auto &a : dp[i]) { int sum = a.first, cnt = a.second; dp[i + 1][sum + nums[i]] += cnt; dp[i + 1][sum - nums[i]] += cnt; } } return dp[n][S]; } }; |
C++:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | class Solution { public : int findTargetSumWays(vector< int >& nums, int S) { unordered_map< int , int > dp; dp[0] = 1; for ( int num : nums) { unordered_map< int , int > t; for ( auto a : dp) { int sum = a.first, cnt = a.second; t[sum + num] += cnt; t[sum - num] += cnt; } dp = t; } return dp[S]; } }; |
类似题目:
[LeetCode] 282. Expression Add Operators 表达式增加操作符
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构