From e0b0078eea56b6af0e18487e428fa78ff93313ec Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Fri, 19 Jan 2024 10:02:55 +0800 Subject: [PATCH] =?UTF-8?q?Update=200494.=E7=9B=AE=E6=A0=87=E5=92=8C?= =?UTF-8?q?=EF=BC=8C=E6=B7=BB=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0494.目标和.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/problems/0494.目标和.md b/problems/0494.目标和.md index 2d38b4d0..e7a05d45 100644 --- a/problems/0494.目标和.md +++ b/problems/0494.目标和.md @@ -585,6 +585,33 @@ impl Solution { } } ``` +### C# +```csharp +public class Solution +{ + public int FindTargetSumWays(int[] nums, int target) + { + int sum = 0; + foreach (int num in nums) + { + sum += num; + } + if (Math.Abs(target) > sum) return 0; + if ((sum + target) % 2 == 1) return 0; + int bagSize = (sum + target) / 2; + int[] dp = new int[bagSize + 1]; + dp[0] = 1; + for (int i = 0; i < nums.Length; i++) + { + for (int j = bagSize; j >= nums[i]; j--) + { + dp[j] += dp[j - nums[i]]; + } + } + return dp[bagSize]; + } +} +```