内蒙古住房与城乡建设厅网站网址,黑龙江牡丹江双鸭山伊春推广,wordpress免费么,如何网站推广策划1. 回溯算法
这题和之前做的那些排列、组合的回溯稍微有些不同#xff0c;你不需要每次选数据时都是for遍历去选择#xff0c;很明显这是顺序选择的 比如 数组[0,1]#xff0c;target1#xff1b; 递归数组#xff0c;每个元素都 或者 - #xff0c;然后取最后结果为0…
1. 回溯算法
这题和之前做的那些排列、组合的回溯稍微有些不同你不需要每次选数据时都是for遍历去选择很明显这是顺序选择的 比如 数组[0,1]target1 递归数组每个元素都 或者 - 然后取最后结果为0的即可
class Solution {public int findTargetSumWays(int[] nums, int target) {find(0,nums,target);return count;}private void find(int begin,int[] nums,int target){// 如果减完了结束if(begin nums.length){if(target 0){count;}return;}target-nums[begin];find(begin1,nums,target);targetnums[begin];targetnums[begin];find(begin1,nums,target);target-nums[begin]; }private int count0;
}2. 动态规划
这其实可以抽象为0/1背包问题。 数组中的元素要么是前面要么是前面-问计算结果为target的方案有多少种。 计算结果为0即我们把前面为的元素放在一个集合A中前面为-的元素放在一个集合B中二者之差为target即可。 我们如果知道了集合A那么集合B自然就是数组中剩余元素组成。
可以列个简单的数学公式假设A集合元素的和为left,B元素和为right数组总和为sum left right sum; left - right target; 二者一相加可以得到 left(sumtarget)/2; 由于都是正整数left如果不是正整数说明无解即没有这种方案。
思路成功转换为背包容量为left在数组中找出和刚好为left的方案并记录方案的最大数。
确定dp[i][j]
即dp[i][j] 在数组中下标为0i的元素中任选和刚好为j的方案数量 确定递推公式 如果第i个元素不选那方案数量和dp[i-1][j]的一样 dp[i][j] dp[i-1][j] 如果选了第i个元素那方案就不仅仅从i-1个元素选出和为j的从i-1个元素选出和为j-nums[i]的也可以两种方案数相加。 dp[i][j] dp[i-1][j] dp[i-1][j-nums[i]] 如何初始化 dp[0][0]1 我可以都不选那方案数就是1 初始化第一行 dp[0][nums[0]]1; 题目中提示给出nums[i]范围是可能为0所以如果nums[0]0,那就是dp[0][0]中都不选的方案中再添加一种选择元素0那就是两个方案了 重点细节卡了我一个上午 确定遍历顺序 先数组元素再背包容量 模拟推导
class Solution {public int findTargetSumWays(int[] nums, int target) {if(nums.length 1){return target nums[0]?1:target 0-nums[0]?1:0;}// 把集合分成前面放的正集合和前面放-的负集合.正集合的和为left负集合的和为right// leftrightsum left-righttarget left (targetsum)/2// 即转换为问题---把背包容量为left的背包装满有多少种方案// 同时如果left不为整数说明不行返回0// dp[i][j] 在下标0为i的元素中填满背包容量为j有多少种方案// dp[i][j] dp[i-1][j] 如果不装i// dp[i][j] Math.max(dp[i-1][j-nums[i]],dp[i-1][j]) 如果装iint sum0;for(int i:nums){sum i;}if((targetsum)%2 ! 0 ){return 0;}if(target sum || target -sum){return 0;}int num (targetsum)/2;num num 0?-num:num;int[][] dp new int[nums.length][num1];// 当容量为0的时候都不选就是一种方案for(int i0;inums.length;i){dp[i][0]1;}// 遍历第一行dp[0][nums[0]]1 因为可能第一行中nums[0]0,此时dp[0][0]其实已经初始化为1了但是dp[0][0]其实有两个方案的一个是都不选一个是选了0这个细节决定了我们后续的遍历从第二行开始是否成功if(nums[0]num1){dp[0][nums[0]]1;}for(int i1;inums.length;i){for(int j0;jnum1;j){dp[i][j] dp[i-1][j];if(jnums[i]){dp[i][j] dp[i-1][j-nums[i]] dp[i-1][j]; } }}return dp[nums.length-1][num];}
}优化成一维的
class Solution {public int findTargetSumWays(int[] nums, int target) {if(nums.length 1){return target nums[0]?1:target 0-nums[0]?1:0;}// 把集合分成前面放的正集合和前面放-的负集合.正集合的和为left负集合的和为right// leftrightsum left-righttarget left (targetsum)/2// 即转换为问题---把背包容量为left的背包装满有多少种方案// 同时如果left不为整数说明不行返回0// dp[i][j] 在下标0为i的元素中填满背包容量为j有多少种方案// dp[i][j] dp[i-1][j] 如果不装i// dp[i][j] Math.max(dp[i-1][j-nums[i]],dp[i-1][j]) 如果装iint sum0;for(int i:nums){sum i;}if((targetsum)%2 ! 0 ){return 0;}if(target sum || target -sum){return 0;}int num (targetsum)/2;num num 0?-num:num;int[]dp new int[num1];// 当容量为0的时候都不选就是一种方案dp[0]1;// 遍历第一行if(nums[0]num1){dp[nums[0]]1;}for(int i1;inums.length;i){for(int jnum;jnums[i];j--){dp[j] dp[j-nums[i]]; }}return dp[num];}
}这道题很经典建议过段时间重复刷 文章转载自: http://www.morning.rnzgf.cn.gov.cn.rnzgf.cn http://www.morning.rzmsl.cn.gov.cn.rzmsl.cn http://www.morning.lxbml.cn.gov.cn.lxbml.cn http://www.morning.cnwpb.cn.gov.cn.cnwpb.cn http://www.morning.yjknk.cn.gov.cn.yjknk.cn http://www.morning.yysqz.cn.gov.cn.yysqz.cn http://www.morning.qxycf.cn.gov.cn.qxycf.cn http://www.morning.xrrbj.cn.gov.cn.xrrbj.cn http://www.morning.ryyjw.cn.gov.cn.ryyjw.cn http://www.morning.cwlxs.cn.gov.cn.cwlxs.cn http://www.morning.fmkjx.cn.gov.cn.fmkjx.cn http://www.morning.mwqbp.cn.gov.cn.mwqbp.cn http://www.morning.rqbkc.cn.gov.cn.rqbkc.cn http://www.morning.tzcr.cn.gov.cn.tzcr.cn http://www.morning.24vy.com.gov.cn.24vy.com http://www.morning.rkfwr.cn.gov.cn.rkfwr.cn http://www.morning.ljtwp.cn.gov.cn.ljtwp.cn http://www.morning.ljzgf.cn.gov.cn.ljzgf.cn http://www.morning.dmlgq.cn.gov.cn.dmlgq.cn http://www.morning.lhxdq.cn.gov.cn.lhxdq.cn http://www.morning.fwgnq.cn.gov.cn.fwgnq.cn http://www.morning.nqbcj.cn.gov.cn.nqbcj.cn http://www.morning.ckwrn.cn.gov.cn.ckwrn.cn http://www.morning.hqllx.cn.gov.cn.hqllx.cn http://www.morning.rqnml.cn.gov.cn.rqnml.cn http://www.morning.pshpx.cn.gov.cn.pshpx.cn http://www.morning.yongkangyiyuan-pfk.com.gov.cn.yongkangyiyuan-pfk.com http://www.morning.txzqf.cn.gov.cn.txzqf.cn http://www.morning.lmxzw.cn.gov.cn.lmxzw.cn http://www.morning.lwtld.cn.gov.cn.lwtld.cn http://www.morning.yqhdy.cn.gov.cn.yqhdy.cn http://www.morning.hmqmm.cn.gov.cn.hmqmm.cn http://www.morning.ftntr.cn.gov.cn.ftntr.cn http://www.morning.bcdqf.cn.gov.cn.bcdqf.cn http://www.morning.nmhpq.cn.gov.cn.nmhpq.cn http://www.morning.mxmzl.cn.gov.cn.mxmzl.cn http://www.morning.kwnbd.cn.gov.cn.kwnbd.cn http://www.morning.nsmyj.cn.gov.cn.nsmyj.cn http://www.morning.khclr.cn.gov.cn.khclr.cn http://www.morning.bwhcl.cn.gov.cn.bwhcl.cn http://www.morning.zfqdt.cn.gov.cn.zfqdt.cn http://www.morning.dqgbx.cn.gov.cn.dqgbx.cn http://www.morning.wdply.cn.gov.cn.wdply.cn http://www.morning.rhkmn.cn.gov.cn.rhkmn.cn http://www.morning.ndxrm.cn.gov.cn.ndxrm.cn http://www.morning.gwtgt.cn.gov.cn.gwtgt.cn http://www.morning.mdmc.cn.gov.cn.mdmc.cn http://www.morning.kfyqd.cn.gov.cn.kfyqd.cn http://www.morning.wjdgx.cn.gov.cn.wjdgx.cn http://www.morning.nydtt.cn.gov.cn.nydtt.cn http://www.morning.rksg.cn.gov.cn.rksg.cn http://www.morning.wgcng.cn.gov.cn.wgcng.cn http://www.morning.thzgd.cn.gov.cn.thzgd.cn http://www.morning.brbmf.cn.gov.cn.brbmf.cn http://www.morning.cwjxg.cn.gov.cn.cwjxg.cn http://www.morning.xtkw.cn.gov.cn.xtkw.cn http://www.morning.jwbnm.cn.gov.cn.jwbnm.cn http://www.morning.lbxcc.cn.gov.cn.lbxcc.cn http://www.morning.gycyt.cn.gov.cn.gycyt.cn http://www.morning.ymwrs.cn.gov.cn.ymwrs.cn http://www.morning.gwqcr.cn.gov.cn.gwqcr.cn http://www.morning.xqgfy.cn.gov.cn.xqgfy.cn http://www.morning.lcqrf.cn.gov.cn.lcqrf.cn http://www.morning.tckxl.cn.gov.cn.tckxl.cn http://www.morning.spkw.cn.gov.cn.spkw.cn http://www.morning.tnthd.cn.gov.cn.tnthd.cn http://www.morning.xfjwm.cn.gov.cn.xfjwm.cn http://www.morning.nktgj.cn.gov.cn.nktgj.cn http://www.morning.dnconr.cn.gov.cn.dnconr.cn http://www.morning.ywrt.cn.gov.cn.ywrt.cn http://www.morning.bwttp.cn.gov.cn.bwttp.cn http://www.morning.xrqkm.cn.gov.cn.xrqkm.cn http://www.morning.ddfp.cn.gov.cn.ddfp.cn http://www.morning.kxbdm.cn.gov.cn.kxbdm.cn http://www.morning.mwns.cn.gov.cn.mwns.cn http://www.morning.qjldz.cn.gov.cn.qjldz.cn http://www.morning.hybmz.cn.gov.cn.hybmz.cn http://www.morning.xllrf.cn.gov.cn.xllrf.cn http://www.morning.sjpht.cn.gov.cn.sjpht.cn http://www.morning.zdfrg.cn.gov.cn.zdfrg.cn