public class Solution {
public int FindTargetSumWays(int[] nums, int S) {
Queue<int> Q = new Queue<int>(); Q.Enqueue(); var count = ;
var dic = new Dictionary<int, int>();
var dic2 = new Dictionary<int, int>();
for (int i = ; 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 > )
{
var n = Q.Dequeue();
var N = ;
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 - )
{
count = dic[l];
}
else
{
Q.Enqueue(l);
}
}
}
return count;
}
}

https://leetcode.com/problems/target-sum/#/description

补充一个python的实现:

 class Solution:
def findTargetSumWays(self, nums: 'List[int]', S: 'int') -> int:
s = {:}
s2 = {}
for n in nums:
for x in s.keys():
x1 = x + n
x2 = x - n
if x1 in s2.keys():
s2[x1]=s2[x1]+s[x]
else:
s2[x1]=s[x]
if x2 in s2.keys():
s2[x2]=s2[x2]+s[x]
else:
s2[x2]=s[x]
#s.clear()
s = s2.copy()
s2.clear()
if S in s.keys():
return s[S]
else:
return

这道题的主要思想是利用字典存储之前的已经计算过的值,但是做题的时候状态不太好,所以写了很久。

做算法题,头脑不清醒的时候,效率很低,应该休息,把状态调整好再做。

简单介绍一下本题的思路,举例数据nums=[1, 1, 1, 1, 1], S=3。

初始情况下字典s为{0:1},表示值0出现过1次。

遍历nums,取每一个值进行计算,i为遍历的下标:

i=0时,当前值为1,得到0+1和0-1,因此s={1: 1, -1: 1}

i=1时,当前值为1,在之前的基础上分别计算+1和-1,有1+1==2,1-1==0,-1+1==0,-1-1==-2,因此s={2: 1, 0: 2, -2: 1}

i=2时,在之前基础上继续计算每一个值+1和-1,得到s={3: 1, 1: 3, -1: 3, -3: 1}

i=3时,s={4: 1, 2: 4, 0: 6, -2: 4, -4: 1}

i=4时,s={5: 1, 3: 5, 1: 10, -1: 10, -3: 5, -5: 1}

遍历完毕,目标S=3,则s[3]的值是5,表示有5种情况可以得到运算结果为3。

leetcode494的更多相关文章

  1. [Swift]LeetCode494. 目标和 | Target Sum

    You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have 2 symb ...

  2. LeetCode-494. Target Sum(DFS&DP)

    You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have 2 symb ...

  3. leetcode494. 目标和

    给定一个非负整数数组,a1, a2, ..., an, 和一个目标数,S.现在你有两个符号 + 和 -.对于数组中的任意一个整数,你都可以从 + 或 -中选择一个符号添加在前面. 返回可以使最终数组和 ...

  4. [Leetcode] DP -- Target Sum

    You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have 2 symb ...

随机推荐

  1. 利用Java获取ip地址

    方法1 public static String getIp2(HttpServletRequest request) { String ip = request.getHeader("X- ...

  2. 自用IP查询网址 - 地址 - 归属地 - 地理位置 - 2017.5

    下面速度较快排行 http://city.ip138.com/ip2city.asp http://1212.ip138.com/ic.asp http://www.taobao.com/help/g ...

  3. Oracle(二)在 Mysql 的基础上学习 Oracle

    毕竟我是先学的mysql,对数据库的一切认知都会有一个先入为主的思想在里面,如果不搞清楚其中的异同,我感觉Oracle我是学不会 了,甚至会把它们混淆.那么,不会mysql的没必要往下看了. 下边第一 ...

  4. web(七)css的语法规则、注释

    css的语法规则:特殊的css语法标识. !important:当使用多种方式设定标签样式时,设定样式渲染的应用优先权,声明在取值之后. .important { color: red !import ...

  5. LADP(Lightweight Directory Access Protocol)轻量目录访问协议~小知识

    What is LDAP and how does it work(implementation)? LDAP stands for “Lightweight Directory Access Pro ...

  6. ArcMap 图层无法编辑

    原因一.图层被其他程序占用 解决方法:关闭与之相关的程序与服务 原因二.没有开启编辑 解决方法:打开编辑器工具>>选项>>版本管理>>勾选或取消勾选编辑数据库版本并 ...

  7. jq实时监测输入框内容改变

    $(document) .on('input propertychange','#telInput',function (e) { if (e.type === "input" | ...

  8. LeetCode - Online Election

    In an election, the i-th vote was cast for persons[i] at time times[i]. Now, we would like to implem ...

  9. Python shelve 模块

    使用json或者pickle持久化数据,能dump多次,但load的话只能取到最新的dump, 因为先前的数据已经被后面dump的数据覆盖掉了. 如果想要实现dump多次不被覆盖,就可以想到使用she ...

  10. TableLayoutPanel 动态添加 行 列

       //添加行 横排 ++this.tbPnl.RowCount; this.tbPnl.RowStyles.Add(new System.Windows.Forms.RowStyle(System ...