[抄题]:

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.

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

种类:看似用DP,但是其实很麻烦

[一句话思路]:

用DFS也能求出种类

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 理解一下退出条件:符合sum = target就count++,达到长度要求就退出

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

[复杂度]:Time complexity: O(每一个都试试加减法 2^n) Space complexity: O(1)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[算法思想:递归/分治/贪心]:递归

[关键模板化代码]:

数组名、目标、位置、当前和

public void dfs(int[] nums, int target, int pos, int sum) {
//exit
if (pos == nums.length) {
if (sum == target) count++;
return ;
}

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

282. Expression Add Operators

[代码风格] :

class Solution {
int count = 0; public int findTargetSumWays(int[] nums, int S) {
//cc
if (nums == null || nums.length == 0) return 0; //dfs
dfs(nums, S, 0, 0); //return
return count;
} public void dfs(int[] nums, int target, int pos, int sum) {
//exit
if (pos == nums.length) {
if (sum == target) count++;
return ;
} //dfs
dfs(nums, target, pos + 1, sum + nums[pos]);
dfs(nums, target, pos + 1, sum - nums[pos]);
}
}

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. 【LeetCode】494. Target Sum 解题报告(Python & C++)

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

  5. 494. Target Sum

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

  6. 494. Target Sum - Unsolved

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

  7. 494 Target Sum 目标和

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

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

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

  9. 【leetcode】494. Target Sum

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

随机推荐

  1. new与malloc的区别,以及内存分配浅析

      从函数声明上可以看出.malloc 和 new 至少有两个不同: new 返回指定类型的指针,并且可以自动计算所需要大小.比如: 1 2 3 int *p; p = new int; //返回类型 ...

  2. Docker的一些常用

    日常使用的一些命令 1234567891011121314 docker pull mysql:tags // 拉mysql的tag版本 docker run -it -p(端口映射-主机端口:容器端 ...

  3. 面向对象(PHP学习)

    在对超大型项目的开发过程中,如果使用面向过程地开发,代码量是非常的庞大,这将大量的用到判断和循环嵌套, 和很多很相似的代码,不仅使项目代码量更加的庞大,还不利于开发,重用及维护. 面向对象就能很好的解 ...

  4. C语言中可变形参简单实例

    以下程序主要包括三个主要函数: 一个最简单的可变形参函数实例: 一个简单的printf功能的实例: 一个打印字符串函数(辅助): 其中myPrintf函数,实现了printf的部分简单功能,并没有去实 ...

  5. Win10系统WMIProviderHost进程占用CPU过高

    “WMI Provider Host“占用了过多CPU资源导致系统卡顿,该如何解决这个问题呢? 解决方法: 可以尝试关闭Windows防火墙服务来解决这个问题. 1.按住win+R,输入service ...

  6. HTML5的LocalStorage和sessionStorage的使用

    本文转载自:http://www.cnblogs.com/qiutianlidehanxing-blog/p/5953746.html html5中的Web Storage包括了两种存储方式:sess ...

  7. java多线程(1) 线程的基本概念

    一.线程的基本概念: 线程是一个程序里面不同的执行路径. 程序里面不同的执行路径,每一个分支都是一个线程.   进程:静态的概念.机器上的一个class文件,机器上的一个exe文件,这叫一个进程. 机 ...

  8. python多标签分类模版

    from sklearn.multioutput import MultiOutputClassifier from sklearn.ensemble import RandomForestClass ...

  9. 第三章 k8s cluster环境创建

    1  用如下方法安装指定版本的docker,但是我的环境会报错 # 安装rpm apt install rpm # 下载 RPM 包, docker 版本 wget https://download. ...

  10. Easyui combotree 获取自定义ID属性方法

    1.设置属性 <input id="cc" class="easyui-combotree" data-options="url:'tree_d ...