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:
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也能求出种类
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 理解一下退出条件:符合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 添加标点符号求和的更多相关文章
- 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 ...
- LC 494. Target Sum
问题描述 You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have 2 ...
- [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 ...
- 【LeetCode】494. Target Sum 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- 494. Target Sum
You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have 2 symb ...
- 494. Target Sum - Unsolved
https://leetcode.com/problems/target-sum/#/description You are given a list of non-negative integers ...
- 494 Target Sum 目标和
给定一个非负整数数组,a1, a2, ..., an, 和一个目标数,S.现在你有两个符号 + 和 -.对于数组中的任意一个整数,你都可以从 + 或 -中选择一个符号添加在前面.返回可以使最终数组和为 ...
- Leetcode 494 Target Sum 动态规划 背包+滚动数据
这是一道水题,作为没有货的水货楼主如是说. 题意:已知一个数组nums {a1,a2,a3,.....,an}(其中0<ai <=1000(1<=k<=n, n<=20) ...
- 【leetcode】494. Target Sum
题目如下: 解题思路:这题可以用动态规划来做.记dp[i][j] = x,表示使用nums的第0个到第i个之间的所有元素得到数值j有x种方法,那么很容易得到递推关系式,dp[i][j] = dp[i- ...
随机推荐
- cookie控制登陆时间
使用cookie实现永久登陆 1,在cookie里面保存账号密码然后和数据库核对(由于我没有使用数据库,就不用了 2,在cookie里面保存时间戳和账号使用加密解密(我也没有使用时间戳 思路,requ ...
- php mysql apache字符集(二) (转)
1 MYSQL中的字符集概念 Mysql的字符集里有两个概念,一个是"Character set(字符集)",另一个是"Collations".1.1 Col ...
- (一)Fiddler的介绍和安装
一.Fiddler的介绍和安装 Fildder是一款免费的web调试代理工具,支持任何浏览器.系统或平台. 官网地址:https://www.telerik.com/fiddler Fiddler原理 ...
- autoreconf报错LC_ALL和LANGUAGE未设置
报错提示: merlin@ubuntu:/opt/smbshared/projects/x86test/openvpn/openvpn-release-2.4$ autoreconf -i -v -f ...
- 确保nginx安全的10个技巧
Nginx是当今最流行的Web服务器之一.它为世界上7%的web流量提供服务而且正在以惊人的速度增长.它是个让人惊奇的服务器,我愿意部署它. 下面是一个常见安全陷阱和解决方案的列表,它可以辅助来确保你 ...
- 关于FPGA供电
FPGA是一种多电源需求的芯片,主要有3种电源需求: VCCINT:核心工作电压,PCI Express (PCIe) 硬核IP 模块和收发器物理编码子层(PCS) 电源.一般电压都很低,目前常用的F ...
- windows下编译nginx+nginx_rtmp_modue(vs2013)
阅读官方编译windows版本的方法 http://nginx.org/en/docs/howto_build_on_win32.html 我的环境 Windows 7 Ultimate 64,Vis ...
- offsetTop/offsetHeight scrollTop/scrollHeight 的区别
offsetTop/offsetHeight scrollTop/scrollHeight 这几个属性困扰了我N久,这次一定要搞定. 假设 obj 为某个 HTML 控件. obj.offset ...
- 传输类型为 "multipart/form-data" 的传送写法 (上传文件 和图片)
一: 传字符的情况: 抓包数据: 传输的数据: python-request写法: 二:上传图片的情况:
- python实战——文本挖掘+xgboost预测+数据处理+准确度计算整合版
if __name__=="__main__": '''============================先导入数据============================= ...