首页 > 代码库 > leetcode494
leetcode494
public class Solution { public int FindTargetSumWays(int[] nums, int S) { Queue<int> Q = new Queue<int>(); Q.Enqueue(0); var count = 0; var dic = new Dictionary<int, int>(); var dic2 = new Dictionary<int, int>(); for (int i = 0; i < nums.Length; i++) { var num = nums[i]; dic2.Clear(); foreach (var d in dic) { dic2.Add(d.Key, d.Value); } dic.Clear(); while (Q.Count > 0) { var n = Q.Dequeue(); var N = 1; if (dic2.ContainsKey(n)) { N = dic2[n]; } if (!dic.ContainsKey(n + num)) { dic.Add(n + num, N); } else { dic[n + num] += N; } if (!dic.ContainsKey(n - num)) { dic.Add(n - num, N); } else { dic[n - num] += N; } } foreach (var l in dic.Keys) { if (l == S && i == nums.Length - 1) { count = dic[l]; } else { Q.Enqueue(l); } } } return count; } }
https://leetcode.com/problems/target-sum/#/description
leetcode494
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。