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:

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.

改变一组数的正负号使得它们的和为一给定数

C++(1056ms):dfs

 class Solution {
public:
int findTargetSumWays(vector<int>& nums, int S) {
return dfs(nums,,S) ;
} int dfs(vector<int>& nums, int start , int S){
if(start == nums.size()){
return S == ? : ;
} return dfs(nums,start+,S + nums[start]) + dfs(nums,start+,S - nums[start]) ;
}
};

494. Target Sum的更多相关文章

  1. LN : leetcode 494 Target Sum

    lc 494 Target Sum 494 Target Sum You are given a list of non-negative integers, a1, a2, ..., an, and ...

  2. LC 494. Target Sum

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

  3. [LeetCode] 494. Target Sum 目标和

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

  4. 494. Target Sum - Unsolved

    https://leetcode.com/problems/target-sum/#/description You are given a list of non-negative integers ...

  5. 494. Target Sum 添加标点符号求和

    [抄题]: You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have ...

  6. 【LeetCode】494. Target Sum 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...

  7. Leetcode 494 Target Sum 动态规划 背包+滚动数据

    这是一道水题,作为没有货的水货楼主如是说. 题意:已知一个数组nums {a1,a2,a3,.....,an}(其中0<ai <=1000(1<=k<=n, n<=20) ...

  8. 494 Target Sum 目标和

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

  9. 【leetcode】494. Target Sum

    题目如下: 解题思路:这题可以用动态规划来做.记dp[i][j] = x,表示使用nums的第0个到第i个之间的所有元素得到数值j有x种方法,那么很容易得到递推关系式,dp[i][j] = dp[i- ...

随机推荐

  1. SSH命令行管理文件

    1.从服务器上下载文件 ssh root@13.111.122.133 2.从服务器上下载文件 scp username@servername:/path/filename /var/www/loca ...

  2. FAT文件系统规范v1.03学习笔记---3.根目录区之FAT目录项结构

    1.前言 本文主要是对Microsoft Extensible Firmware Initiative FAT32 File System Specification中文翻译版的学习笔记. 每个FAT ...

  3. 鸟哥Linux私房菜基础学习篇学习笔记1

    鸟哥Linux私房菜基础学习篇学习笔记1 第三章 主导分区(MBR),当系统在开机的时候会主动去读取这个区块的内容,必须对硬盘进行分区,这样硬盘才能被有效地使用. 所谓的分区只是针对64Bytes的分 ...

  4. Linux基础知识之bashrc和profile的用途和区别

    使用终端ssh登录Linux操作系统的控制台后,会出现一个提示符号(例如:#或~),在这个提示符号之后可以输入命令,Linux根据输入的命令会做回应,这一连串的动作是由一个所谓的Shell来做处理. ...

  5. thinkphp中的内置操作数据库与mysql中的函数汇总

    8.4.4 Model类getModelName() 获取当前Model的名称getTableName() 获取当前Model的数据表名称switchModel(type,vars=array()) ...

  6. [Linux]PHP-FPM与NGINX的两种通讯方式

    一.通过监听TCP端口通讯 php-fpm.d/www.conf ; The address on which to accept FastCGI requests. ; Valid syntaxes ...

  7. pip的常用命令

    前言 pip作为Python的御用包管理工具有着强大的功能,但是许多命令需要我们使用的时候借助搜索引擎查找(尤其是我), 于是我想将我使用到的命令整合下来,以后不用麻烦去找了,也希望能给你带来帮助.文 ...

  8. Confluence 6 自定义管理员联系信息

    你可以自定义在 联系站点管理员(Contact Site Administrators)页面中显示的消息. 希望编辑这个管理员联系消息: 在屏幕的右上角单击 控制台按钮 ,然后选择 General C ...

  9. kernel_size

    kernel_size=(1,3)[flag] if flag==True:kernel_size=3 else: kernel_size=1

  10. Spark SQL访问PostgreSQL

    随着Spark SQL的正式发布,以及它对DataFrame的支持,它可能会取代HIVE成为越来越重要的针对结构型数据进行分析的平台.在博客文章What’s new for Spark SQL in ...