Leetcode-494-Target-Sum

494. Target Sum[Medium](Leetcode每日一题-2021.06.07)--抄答案 Problem You are given an integer array nums and an integer target. You want to build an expression out of nums by adding one of the symbols ‘+’ and ‘-’ before each integer in nums and then concatenate all the integers. For example, if nums = [2, 1], you c 阅读详情

Leetcode-494-Target-Sum

题目如下:

You are given a list of non-negative integers, a1, a2, …, an, and a target, S. Now you have 2 symbols + and -. For each integer, you should choose one from + and - as its new symbol.
Find out how many ways to assign symbols to make sum of integers equal to target S.

Example 1:

 

1

2

3

4

5

6

7

8

9

10

 

> Input: nums is [1, 1, 1, 1, 1], S is 3.

> Output: 5

> Explanation:

> -1+1+1+1+1 = 3

> +1-1+1+1+1 = 3

> +1+1-1+1+1 = 3

> +1+1+1-1+1 = 3

> +1+1+1+1-1 = 3

> There are 5 ways to assign symbols to make the sum of nums be target 3.

>

 

Note:

  1. The length of the given array is positive and will not exceed 20.
  2. The sum of elements in the given array will not exceed 1000.
  3. Your output answer is guaranteed to be fitted in a 32-bit integer.

题目大意

给定一个正数数组以及一个整数S,现在有两个符号+和-,对于数组里面的每一个数字,你可以选择+或者-作为它的新符号,找出所有选择符号的方式,使这些数字和为S。简单来说,给定一个数组,每个数可以加或者减,求出所有能使这些数字加减和为S的组合数。如{1,1,1,1,1},S为3,那么一共有5种方法使得这些数字和为3:

-1+1+1+1+1 = 3
+1-1+1+1+1 = 3
+1+1-1+1+1 = 3
+1+1+1-1+1 = 3
+1+1+1+1-1 = 3

思路

假设数组中所有数字之和为sum。根据使用的符号不同,我们可以将数组内数字分为2组P和N,如数组{1,2,3},目标为0,那么显然有如下选择方式:

+1+2-3 = 0

那么其中P={1,2},N={3}。那么sum(P)-sum(N)=S,并且sum(P)+sum(N)=sum。由此可以推出S+sum=sum(P)-sum(N)+sum(P)+sum(N)=2*sum(P)。

这样我们就把原来的问题转换成了一个新的问题,在整个数组中,能否找到一些数字的和为(S+sum)/2,这就成了一个最经典的0-1背包问题,这样的话就简单了很多。

这里有一个需要注意的地方,(S+sum)/2这个数必须为偶数,否则无法得到,其次,S也必须小于sum,否则也无法求得。

这个算法的空间复杂度不定,取决于S和sum,不过题目限制了sum < 1000,那么空间复杂度为O(sum)。至于时间复杂度也取决于sum,时间复杂度是O(sum*N2)。Java版的代码如下:

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

 

public int findTargetSumWays(int[] nums, int S) {

int sum = 0;

for (int i = 0; i < nums.length; i ++){

sum += nums[i];

}

if (S > sum || (S + sum) % 2 != 0){

return 0;

}

sum = (S + sum) / 2;

int [] dp = new int[sum + 1];

dp[0] = 1;

for (int i = 0; i < nums.length; i ++){

for (int j = sum; j >= nums[i]; j --){

dp[j] += dp[j - nums[i]];

}

}

return dp[sum];

}

提交结果如下:

运行结果

运行结果

 

总结

最近在刷Leetcode上的tag为动态规划的题,做了那么多其实动态规划的思想一直没变,最重要的是找到状态、状态转移公式以及转移状态的条件。当然,有的时候还需要一些数学推导,把当前不熟悉的问题转化成熟悉的问题。
```
原文链接:http://KingsFish.github.io/2017/08/22/Leetcode-494-Target-Sum/

泛微Ecology9单点登录实战指南:从Token获取到页面跳转 本文提供了一份详尽的泛微Ecology9单点登录实战指南,重点解析了Token跳转式SSO的核心流程。内容涵盖从理解关键参数(如appid、loginid)与一次性Token机制,到通过代码示例演示如何正确调用接口获取Token,并详细说明了拼接不同场景跳转地址的完整规则与安全实践,旨在帮助开发者高效、稳定地实现业务系统与OA门户的无缝集成。 阅读详情

相关推荐

unity中VideoPlayer播放视频,使用Shader除去黑底且无黑边

1. 创建一个Shader 和 一个 材质球;Shader中复制如下代码:(此Shader代码为“ChromaKeyKit”插件中的ChromaKey_Alpa_Genera代码,但因只需要除去视频黑底而挂一个插件有点不合适) // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)' Shader "ChromaKeyKit/ChromaKey/ChromaKey_Alpha_General"

weixin_42065078的博客 1747

Leetcode494. 求解目标和个数

求解子集和问题

obrcnh的博客 1113

避坑指南:新手爬百度图片3个致命错,改完批量保存本地(亲测有效)

新手爬百度图片的核心是“绕开静态HTML,抓准API接口+处理反爬+提取原图”,改完本文提到的3个核心错,就能稳定批量保存高清图片。这套思路可复用在其他图片网站(如必应图片、360图片),只需修改API接口和参数即可。如果想进一步优化,可添加代理IP池、多线程下载(新手不建议,容易触发反爬)、图片去重等功能!

专注于Python爬虫开发,分享爬虫技巧、项目实战与反爬经验,使用Scrapy、BeautifulSoup等工具,解决数据抓取难题。 1587

leetcode - 494. 目标和

494. 目标和 ———————————————————————————————————————————— 给定一个非负整数数组,a1, a2, …, an, 和一个目标数,S。现在你有两个符号 + 和 -。对于数组中的任意一个整数,你都可以从 + 或 -中选择一个符号添加在前面。返回可以使最终数组和为目标数 S 的所有添加符号的方法数。 示例 1: 输入: nums: [1, 1, 1, 1, ...

然后就去远行 466

手把手教你学simulink(44.1)--串励直流电动机场景示例:基于Simulink开发一个串励直流电动机电压控制仿真模型

高启动转矩:由于励磁绕组和电枢绕组串联连接,电机在启动时可以产生较大的转矩。速度随负载变化:由于励磁电流和电枢电流是同一电流,电机的速度会随着负载的变化而显著变化。适用于重载启动:适合用于需要高启动转矩的应用,如起重机、牵引系统等。

MHD0815的博客 340

静态时序分析(STA)基本概念

静态时序分析中常见概念(fasle Path,multicycle path等)

m0_61544122的博客 2256

LeetCode494. Target Sum题解

You are given a list of non-negative integers, a1, a2, …, an, and a target, S. Now you have 2 symbols + and -. For each integer, you should choose one from + and - as its new symbol.

SYSU_LBY的博客 2054

Lammps纳米压痕接触面积—Matlab接触面积计算(两种方法)

Lammps纳米压痕接触面积计算

Rockyoung 5449

leetcode494 目标和

给定一个非负整数数组,a1, a2, ..., an, 和一个目标数,S。现在你有两个符号 + 和 -。对于数组中的任意一个整数,你都可以从 + 或 -中选择一个符号添加在前面。 返回可以使最终数组和为目标数 S 的所有添加符号的方法数。 示例 1: 输入: nums: [1, 1, 1, 1, 1], S: 3 输出: 5 解释: -1+1+1+1+1 = 3 +1-1+1+1+1 ...

斜月&三星 472

从生产订单到整车交付,SAP 离散制造在汽车行业里的真实落点

汽车行业谈数字化,很容易一上来就谈智能工厂、数字孪生、AI 排产、无人仓、视觉质检。可落到 SAP 项目现场,真正把工厂撑起来的,往往还是几个很朴素的问题,客户订单来了以后,BOM 能不能准确展开,物料能不能准时到线,产能有没有被过度承诺,生产订单能不能被车间正确执行,质量问题能不能追溯,成本能不能按工单和批次沉淀下来。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 113

路径规划 | 图解A*、Dijkstra、GBFS算法的异同(附C++/Python/Matlab仿真)

图解A*、Dijkstra、GBFS算法原理,分析三种算法异同,提供C++/Python/Matlab仿真实现

FRIGIDWINTER的博客 2万+

Leetcode 494:目标和(最详细的解法!!!)

给定一个非负整数数组,a1, a2, …, an, 和一个目标数,S。现在你有两个符号 + 和 -。对于数组中的任意一个整数,你都可以从 + 或 -中选择一个符号添加在前面。 返回可以使最终数组和为目标数 S 的所有添加符号的方法数。 示例 1: 输入: nums: [1, 1, 1, 1, 1], S: 3 输出: 5 解释: -1+1+1+1+1 = 3 +1-1+1+1+1 = 3 +1...

coordinate的博客 1万+

NSIS教程(2): 传统用户界面(Classic UI)

NSIS教程: 《NSIS用户手册 中文版》 《NSIS图文教程集锦 - 轻狂志》 本文以一个完整的、传统界面的网易云音乐安装包的脚本为示例,来介绍NSIS传统用户界面的开发流程。 NSIS脚本 # 单行注释用井号"#"或分号";",跨行注释用可以用C/C++中注释语法。 # 使用!define定义一些常量(如安装包的版本号等) # 对常量或变量的引用可以使用如${PROD

while(1) { smile(); } 8万+

Java | LeetCode | 494. Target Sum | 背包问题 | 动态规划

494. Target Sum | 背包问题 | 动态规划 问题描述 You are given a list of non-negative integers, a1, a2, …, an, and a target, S. Now you have 2 symbols + and -. For each integer, you should choose one from + and - a...

肖胖孩de部落格 654

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

LeetCode(494):目标和 Target Sum(Java)

2019.10.24 #程序员笔试必备# LeetCode 从零单刷个人笔记整理(持续更新) github:https://github.com/ChopinXBP/LeetCode-Babel 这题表面上是一道递归问题,好在递归层数也不是特别多,每层分支也不是很爆炸,所以直接递归也不会超时。不过这题有个更巧妙的方法,能够转换成一个01背包问题。 将数据N看成AB两个部分,A符号全取正,B符号全...

NJU_ChopinXBP的博客 418

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

每周LeetCode算法题(十八)494. Target Sum

每周LeetCode算法题(十八)题目: 494. Target Sum You are given a list of non-negative integers, a1, a2, …, an, and a target, S. Now you have 2 symbols + and -. For each integer, you should choose one from + and

JacKnights的博客 346

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

【Leetcode】494. 目标和(Target Sum)

Leetcode - 494 Target Sum (Medium) 题目描述:给定一个正整数数组,添加正负号使得最终和为 target,求共有几种添加正负号的方式。 Input: nums is [1, 1, 1, 1, 1], S is 3. Output: 5 Explanation: -1+1+1+1+1 = 3 +1-1+1+1+1 = 3 +1+1-1+1+1 = 3 +1+1+...

厉兵秣码 461

Leetcode494. Target Sum

Leetcode494. Target Sum题目:You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have 2 symbols + and -. For each integer, you should choose one from + and - as its n

mohuak的博客 222

LeetCode Top 100 Liked Questions 494. Target Sum (Java版; Medium)

welcome to my blog LeetCode Top 100 Liked Questions 494. Target Sum (Java版; Medium) 题目描述 You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have 2 symbols + and -...

littlehaes的博客 227

【Leetcode-Medium-494】Target Sum

【Leetcode-Medium-494】Target Sum题目You are given a list of non-negative integers, a1, a2, …, an, and a target, S. Now you have 2 symbols + and -. For each integer, you should choose one from + and - as i

LYZ 912

Leetcode 494 Target Sum

LC 494 Target Sum Question: You are given a list of non-negative integers, a1, a2, …, an, and a target, S. Now you have 2 symbols “+” and “-” . For each integer, you should choose one from “+” and “-”...

weixin_43876597的博客 158

【Leetcode】494. Target Sum

题目地址: https://leetcode.com/problems/target-sum/ 给定一个长nnn数组AAA,再给定一个目标值ttt,可以在数组的每个数前加上正号或者负号,问能得到的和为ttt的方案有多少种。 可以用记忆化搜索。设从A[i]A[i]A[i]开始向后,当前和是sss,达到ttt的方案数有f[i][s]f[i][s]f[i][s]种,那么f[i][s]=f[i+1][s+A[i]]+f[i+1][s−A[i]]f[i][s]=f[i+1][s+A[i]]+f[i+1][s-A[i]

数学、算法爱好者的博客 361

[LeetCode]494. Target Sum

https://leetcode.com/problems/target-sum/ 给一个int数组,只能用“+”和“-”,判断计算后有多少种情况得到指定target 内层循环要用next数组保存下一状态,如果都用dp保存的话,下一状态对于当前未遍历到的状态会有污染。 public class Solution { public int findTargetSu

gqk289的专栏 3013

Leetcode算法学习日志-494 Target Sum

Leetcode 494 Target Sum 题目原文 You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have 2 symbols + and -. For each integer, you should choose one from + and

Zarlove的博客 581

LeetCode 494 Target Sum 题解

题意简述:给定一个非负数组nums和目标值S,你需要在每个数之前加上加号或减号组成表达式。求令表达式的值恰好为S的方法总共有多少种。 输入:非负数组nums,目标值S 输出:使表达式的值恰好为S的方法总数。 示例:对于数组[1,1,1,1,1],S=3,那么总共有5种方法:令其中1个1取-号,剩余4个1取加号即可。题解: 表面上看,每个位置都是考虑取加法还是取减法。我们不妨换个思路:记sum

MonoTali的博客 1028

Leetcode——494. Target Sum

题目描述 You are given a list of non-negative integers, a1, a2, …, an, and a target, S. Now you have 2 symbols + and -. For each integer, you should choose one from + and - as its new symbol. Find out how...

Johnsonjjj的博客 373
上一篇: LeetCode 39. Combination Sum 题解
下一篇: JDK 8 HashMap 源码解析
TaiSung
博客等级 码龄15年 49粉丝 132原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值