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. JavaScript实现本地图片上传前进行裁剪预览

    本项目支持IE8+,测试环境IE8,IE9,IE10,IE11,Chrome,FireFox测试通过 另:本项目并不支持Vue,React等,也不建议,引入JQuery和Vue.React本身提倡的开 ...

  2. SamplesHashtable

    using System; using System.Collections; public class SamplesHashtable { public static void Main() { ...

  3. C# 文件上传下载功能实现 文件管理引擎开发

    Prepare 本文将使用一个NuGet公开的组件技术来实现一个服务器端的文件管理引擎,提供了一些简单的API,来方便的实现文件引擎来对您自己的软件系统的文件进行管理. 在Visual Studio ...

  4. #电脑磁盘分区#新买的电脑一般只有C盘或者C盘和D盘,怎么加多几个盘呢

    新买的电脑一般只有C盘或者C盘和D盘,怎么加多几个盘呢 鼠标右键点击桌面我的电脑选择管理 进入计算机管理.选择磁盘管理 若桌面没有我的电脑,可按win+x键,在快捷菜单栏中点击磁盘管理 通过以上两种w ...

  5. python 进程之间的数据共享

    from multiprocessing import Process,Manager import os def f(d,n): d[os.getpid()] = os.getppid()#对字典d ...

  6. Linux源码安装软件

    Linux环境下 源码编译安装软件 ==== 1. 下载,步骤略 ==== 2. 验证,部分官方下载同时提供签名,MD5,sha1,sha256等校验信息. $ gpg --verify gnupg- ...

  7. springdata笔记

    SpringData整合hibernate CRUD操作: applicationContext.xml: <?xml version="1.0" encoding=&quo ...

  8. pytorch基础

    1.创建一个未初始化矩阵 from __future__ import print_function import torch x = torch.empty(2,3)#uninitialized m ...

  9. LiquiBase 学习

    preconditions mysql database is installed maven has been setted up properly add depedenceies apply p ...

  10. python:用wordcloud生成一个文本的词云

    今天学习了wordcloud库,对<三国演义>生成了词云图片,非常漂亮.就想多尝试几个,结果发现一系列问题.最常出现的一个错误就是"UnicodeDecodeError : .. ...