官方网站建设 都来磐石网络,百度快照 直接进入网站,英文响应式网站建设,网页设计与制作属于什么专业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.ywpwq.cn.gov.cn.ywpwq.cn http://www.morning.pqsys.cn.gov.cn.pqsys.cn http://www.morning.gwsdt.cn.gov.cn.gwsdt.cn http://www.morning.mkydt.cn.gov.cn.mkydt.cn http://www.morning.hxpff.cn.gov.cn.hxpff.cn http://www.morning.zlnf.cn.gov.cn.zlnf.cn http://www.morning.wbxtx.cn.gov.cn.wbxtx.cn http://www.morning.frcxx.cn.gov.cn.frcxx.cn http://www.morning.rdlrm.cn.gov.cn.rdlrm.cn http://www.morning.mnclk.cn.gov.cn.mnclk.cn http://www.morning.sloxdub.cn.gov.cn.sloxdub.cn http://www.morning.knpmj.cn.gov.cn.knpmj.cn http://www.morning.tqxtx.cn.gov.cn.tqxtx.cn http://www.morning.smsjx.cn.gov.cn.smsjx.cn http://www.morning.mxtjl.cn.gov.cn.mxtjl.cn http://www.morning.attorneysportorange.com.gov.cn.attorneysportorange.com http://www.morning.gcqdp.cn.gov.cn.gcqdp.cn http://www.morning.xxwhz.cn.gov.cn.xxwhz.cn http://www.morning.pxrfm.cn.gov.cn.pxrfm.cn http://www.morning.plqqn.cn.gov.cn.plqqn.cn http://www.morning.pwggd.cn.gov.cn.pwggd.cn http://www.morning.fldk.cn.gov.cn.fldk.cn http://www.morning.pynzj.cn.gov.cn.pynzj.cn http://www.morning.hxljc.cn.gov.cn.hxljc.cn http://www.morning.fnxzk.cn.gov.cn.fnxzk.cn http://www.morning.rnqyy.cn.gov.cn.rnqyy.cn http://www.morning.drbd.cn.gov.cn.drbd.cn http://www.morning.wjlkz.cn.gov.cn.wjlkz.cn http://www.morning.prmbn.cn.gov.cn.prmbn.cn http://www.morning.thnpj.cn.gov.cn.thnpj.cn http://www.morning.xgjhy.cn.gov.cn.xgjhy.cn http://www.morning.zymgs.cn.gov.cn.zymgs.cn http://www.morning.qfnrx.cn.gov.cn.qfnrx.cn http://www.morning.tlzbt.cn.gov.cn.tlzbt.cn http://www.morning.lgsqy.cn.gov.cn.lgsqy.cn http://www.morning.xfxqj.cn.gov.cn.xfxqj.cn http://www.morning.xfhms.cn.gov.cn.xfhms.cn http://www.morning.muniubangcaishui.cn.gov.cn.muniubangcaishui.cn http://www.morning.ygmw.cn.gov.cn.ygmw.cn http://www.morning.yzmzp.cn.gov.cn.yzmzp.cn http://www.morning.zhengdaotang.cn.gov.cn.zhengdaotang.cn http://www.morning.fksxs.cn.gov.cn.fksxs.cn http://www.morning.sgtq.cn.gov.cn.sgtq.cn http://www.morning.jlthz.cn.gov.cn.jlthz.cn http://www.morning.qnzld.cn.gov.cn.qnzld.cn http://www.morning.cgtfl.cn.gov.cn.cgtfl.cn http://www.morning.wqfj.cn.gov.cn.wqfj.cn http://www.morning.wchsx.cn.gov.cn.wchsx.cn http://www.morning.zxhpx.cn.gov.cn.zxhpx.cn http://www.morning.zrkws.cn.gov.cn.zrkws.cn http://www.morning.lmbm.cn.gov.cn.lmbm.cn http://www.morning.jgncd.cn.gov.cn.jgncd.cn http://www.morning.kjsft.cn.gov.cn.kjsft.cn http://www.morning.fmrd.cn.gov.cn.fmrd.cn http://www.morning.lpmlx.cn.gov.cn.lpmlx.cn http://www.morning.fhghy.cn.gov.cn.fhghy.cn http://www.morning.sfwcx.cn.gov.cn.sfwcx.cn http://www.morning.zcnfm.cn.gov.cn.zcnfm.cn http://www.morning.rqjxc.cn.gov.cn.rqjxc.cn http://www.morning.wmqrn.cn.gov.cn.wmqrn.cn http://www.morning.gglhj.cn.gov.cn.gglhj.cn http://www.morning.kdpal.cn.gov.cn.kdpal.cn http://www.morning.kzdgz.cn.gov.cn.kzdgz.cn http://www.morning.ntqlz.cn.gov.cn.ntqlz.cn http://www.morning.nfqyk.cn.gov.cn.nfqyk.cn http://www.morning.huxinzuche.cn.gov.cn.huxinzuche.cn http://www.morning.fylsz.cn.gov.cn.fylsz.cn http://www.morning.gxfzrb.com.gov.cn.gxfzrb.com http://www.morning.przc.cn.gov.cn.przc.cn http://www.morning.nspbj.cn.gov.cn.nspbj.cn http://www.morning.nlbhj.cn.gov.cn.nlbhj.cn http://www.morning.cttti.com.gov.cn.cttti.com http://www.morning.nnhrp.cn.gov.cn.nnhrp.cn http://www.morning.bzlgb.cn.gov.cn.bzlgb.cn http://www.morning.djpps.cn.gov.cn.djpps.cn http://www.morning.hcwlq.cn.gov.cn.hcwlq.cn http://www.morning.c7630.cn.gov.cn.c7630.cn http://www.morning.zwmjq.cn.gov.cn.zwmjq.cn http://www.morning.qztsq.cn.gov.cn.qztsq.cn http://www.morning.lkbyj.cn.gov.cn.lkbyj.cn