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] [Lock] [Synchronized VS ReentrantLock]

    Overview java编写多线程程序时,为了保证线程安全,需要对数据进行同步,经常用到的两种同步方式就是synchronized和重入锁ReentrantLock. 相似点 都是加锁方式 都是阻塞 ...

  2. 使用Java API方式的MapReduce练习

    众所周知,hadoop生态圈的多数组件都是使用java开发的. 那么使用Java API方式实现起来,显得要比其它语言效率更高,更原生态. 前面有一个Hadoop学习笔记02_MapReduce练习 ...

  3. python发送邮箱的小项目

    import smtplibfrom email.mime.text import MIMEText receiver = input('输入接受者邮箱\n')subject = input('输入标 ...

  4. cut语法2

    linux每日一命令--cut--按文件大小排序 显示前100行 显示后五列 ll -Sh|head -n 100|cut -d ' ' -f 5- 一.基本语法cut是一个选取命令,以行为单位,用指 ...

  5. 项目配置linux上, 配置文件访问不到

    /** * 读入TXT文件 */public List<String> readFile(String pathName) { List<String> list = new ...

  6. 关于IT行业加班的问题

    众所周知,所有行业中,IT行业加班最为严重,国内比较大的IT公司都有加班文化. 为什么要加班?有的时候加班是为了项目上线,因为正在运行的项目,在晚上的时候访问量是最小的,这个时候做系统更新是损失最小的 ...

  7. React Native 调用 Web3(1.x) 的正确姿势

    1 创建项目 react-native init lm1 cd lm1 2 安装依赖包 yarn add node-libs-browser 3 创建 rn-cli.config.js 脚本 cons ...

  8. 测试那些事儿—postman入门介绍

    1.postman入门介绍 一款功能强大的网页调试与发送网页HTTP请求的工具. 1)模拟各种HTTP请求,如get,post,put,delete 2)测试集合Collection    Colle ...

  9. ViewpagerHandler

    import android.os.AsyncTask; import android.os.Handler; import android.os.Message; import android.su ...

  10. oracle query

    不等值连接查询 员工工资级别 select e.empno,e.ename,e.sal,s.grade from emp e,salgrade s where e.sal between s.losa ...