[抄题]:

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]);
}
}

最新文章

  1. Bzoj1305 [CQOI2009]dance跳舞
  2. android ExpandAbleListView控件
  3. [转]关于WM_NCHITTEST消息
  4. php 父类子类构造函数注意事项
  5. 办理滑铁卢大学(本科)学历认证『微信171922772』UW学位证成绩单使馆认证University of Waterloo
  6. 【JAVASCRIPT】React入门学习-文本渲染
  7. scala的多种集合的使用(8)之队列和栈的操作方法
  8. Guava 源码分析(Cache 原理)
  9. Docker系列06—基于容器制作镜像并上传到Docker Registry
  10. $(document).on('click','.classname',function(){}); VS $('.classname').on('click',function(){});
  11. HAProxy(二):HAProxy的ACL规则实现智能负载均衡详解与示例
  12. oryx 分离&集成
  13. hdoj1075-What Are You Talking About 【map】
  14. pycharm中新建并且运行django
  15. mysql 绿色版安装
  16. 003:MySQL账号创建授权以及Workbench
  17. Python2和Python3之间的区别
  18. [原]Linux 修改时区
  19. Lua脚本认知小结
  20. Python Unicode与中文处理(转)

热门文章

  1. 项目代码部署百度云(使用git部署,node环境)
  2. Unit07: MyBatis框架简介 、 MyBatis基本应用
  3. 手写html表格熟练度练习
  4. Zookeeper--集群管理
  5. cx_Oracle.DatabaseError: ORA-12541: TNS:no listener
  6. 网络 、osi 七层模型、tcp/ip 五层参考
  7. 第十章 Secret & Configmap (中)
  8. wamp环境的安装
  9. 5月9日上课笔记-网页定位、网页动画【HTML5】
  10. Python环境搭建之OpenGL