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. 为何放弃Eclipse,选择IntelliJ IDEA,看完终于明白了

    如果你初次用idea,毫无目的的度娘如何使用IDEA     浪费的将会是大量的时间.一套让你花时间,少走弯路的视频教程(下载地址:https://pan.baidu.com/s/1gfeX3hD) ...

  2. @Configuration的使用

    以下内容转载自:duanxz的spring4.0之二:@Configuration的使用,如有侵权,请联系作者本人予以删除 从Spring3.0,@Configuration用于定义配置类,可替换xm ...

  3. Page 对象详解

    Page 对象 由于网页编译后所创建的类由Page派生而来,因此网页可以直接使用Page对象的属性.方法和事件. Page对象的常用属性 1.IsPostBack(bool类型) 获取一个值,该值指示 ...

  4. 3.2 unittest执行顺序

    3.2 unittest执行顺序 前言很多初学者在使用unittest框架时候,不清楚用例的执行顺序到底是怎样的.对测试类里面的类和方法分不清楚,不知道什么时候执行,什么时候不执行.本篇通过最简单案例 ...

  5. linux 命令失效

    失效的原因 是我在执行命令的时候输入错误了.在网上找了很多的办法都是以下两种方式:  其一:直接在linux命令行界面输入如下,然后回车(导入环境变量,以及shell常见的命令的存放地址):  exp ...

  6. Shell脚本、Shell脚本结构、date命令的用法、变量

    1.Shell脚本: shell是一种脚本语言 目的:可以实现自动化运维,能大大增加运维的效率.2.Shell脚本结构:   #!/bin/bash  以#!/bin/bash开头,即以/bin/ba ...

  7. some learning

    一.windows下迁移access到mysql Windows下 Access 数据 迁移到 Mysql(5.5)数据库 . 具体做法 . 在access的表中选择,文件->导出->保存 ...

  8. 使用Blend设计出符合效果的WPF界面

    之前不会用blend,感觉好难的,但美工给出的效果自己有没办法实现,所以研究了一下blend,感觉没有想象中的那么难 废话不多说,开始界面设计 今天拿到美工给的一个界面效果图 这个界面说实话,还可以吧 ...

  9. 十五、springcloud(一)注册中心Eureka

    1.Eureka的基本架构 a.Eureka Server 提供服务注册和发现 b.Service Provider 服务提供方 将自身服务注册到Eureka,从而使服务消费方能够找到 c.Servi ...

  10. socket资源

    http://www.360doc.com/content/13/1231/16/14919052_341525862.shtml Linux下基于socket多线程并发通信的实现 https://w ...