网站商城模板,成都专业做网站公司有哪些,小型的做网站公司从哪里接的项目,上海技术公司做网站前言
###我做这类文档一个重要的目的还是给正在学习的大家提供方向#xff08;例如想要掌握基础用法#xff0c;该刷哪些题#xff1f;#xff09;我的解析也不会做的非常详细#xff0c;只会提供思路和一些关键点#xff0c;力扣上的大佬们的题解质量是非常非常高滴例如想要掌握基础用法该刷哪些题我的解析也不会做的非常详细只会提供思路和一些关键点力扣上的大佬们的题解质量是非常非常高滴 什么是回溯算法 回溯搜索法是一种搜索方式它通过不断尝试各种可能的选择当发现当前选择不满足条件时就回溯到上一个状态重新进行选择直到找到满足条件的解或者遍历完所有可能的情况。例如在解决迷宫问题时可以使用回溯搜索法从一个起始位置开始尝试不同的方向前进如果遇到死路就回溯到上一个位置重新选择其他方向。 习题
1.组合
题目链接:77. 组合 - 力扣LeetCode
题面:
基本分析:抽象成树的结构进行递归
代码:
class Solution {int len;public ListListInteger combine(int n, int k) {ListListInteger ans new ArrayList();StackInteger stack new Stack();len k;recursion(1,n,ans,stack);return ans;}public void recursion(int start,int end,ListListInteger ans,StackInteger stack){if(stack.size()len){ans.add(new ArrayList(stack));return;}for(int i start;iend - (len - stack.size()) 1;i){stack.push(i);recursion(i1,end,ans,stack);stack.pop();}}
} 2.组合总和III
题目链接:216. 组合总和 III - 力扣LeetCode
题面:
基本分析:和上一题类似只不过多一个判断相加之和
代码:
class Solution {int len;int target;public ListListInteger combinationSum3(int k, int n) {ListListInteger ans new ArrayList();len k;target n;StackInteger stack new Stack();recursion(1,9,stack,ans,0);return ans;}public void recursion(int l,int r,StackInteger stack,ListListInteger ans,int sum){if(sumtarget)return;if(stack.size()len)return;if(sumtargetstack.size()len)ans.add(new ArrayList(stack));for(int i l;ir-(len-stack.size())1;i){stack.push(i);recursion(i1,r,stack,ans,sumi);stack.pop();}}
}
3.电话号码的字母组合
题目链接:17. 电话号码的字母组合 - 力扣LeetCode
题面:
基本分析:暴力递归
代码:
class Solution {char[][] arr {{d},{a},{a,b,c},{d,e,f},{g,h,i},{j,k,l},{m,n,o},{p,q,r,s},{t,u,v},{w,x,y,z}};int count -1;int len;public ListString letterCombinations(String digits) {ListString list new ArrayList();if(digits.equals())return list; char[] brr digits.toCharArray();int n brr.length;len n;char[] crr new char[n];recursion(0,brr,crr,list);return list;}public void recursion(int u,char[] brr,char[] crr,ListString list){if(ulen){list.add(new String(crr));return;}char c brr[u];char[] drr arr[c-0];int m drr.length;for(int i 0;im;i){crr[count]drr[i];recursion(u1,brr,crr,list);count--;}}
} 4.组合总和
题目链接:39. 组合总和 - 力扣LeetCode
题面:
基本分析:注意剪枝来去重
代码:
class Solution {int[] arr;int len;int t;public ListListInteger combinationSum(int[] candidates, int target) {arr candidates;ListListInteger list new ArrayList();ListInteger stack new Stack();len candidates.length;t target;Arrays.sort(arr);recursion(list,stack,0,0);return list;}public void recursion(ListListInteger list,ListInteger stack,int sum,int l){if(sumt){list.add(new ArrayList(stack));return;}for(int i l;ilen;i){if(sumarr[i]t)return;stack.add(arr[i]);recursion(list,stack,sumarr[i],i);stack.remove(stack.size()-1);}}
} 5.组合总和II
题目链接:40. 组合总和 II - 力扣LeetCode
题面:
基本分析:注意重复元素导致的重复,用set是一种解决办法但是会超时
代码:
class Solution {int t;int[] arr;int n;public ListListInteger combinationSum2(int[] candidates, int target) {Arrays.sort(candidates);arr candidates;SetListInteger list new HashSet();ListInteger stack new ArrayList();n arr.length;t target;recursion(list,stack,0,0);return new ArrayList(list);}public void recursion(SetListInteger list,ListInteger stack,int sum,int l){if(sumt){list.add(new ArrayList(stack));return;}for(int i l;in;i){if(sumarr[i]t)return;if (i l arr[i] arr[i - 1]) {continue;}stack.add(arr[i]);recursion(list,stack,sumarr[i],i1);stack.remove(stack.size()-1);}}
} 后言
上面是回溯算法的基本概念和部分习题下一篇会讲解回溯算法的其他相关力扣习题希望有所帮助一同进步共勉
文章转载自: http://www.morning.fgwzl.cn.gov.cn.fgwzl.cn http://www.morning.mkrqh.cn.gov.cn.mkrqh.cn http://www.morning.nqbs.cn.gov.cn.nqbs.cn http://www.morning.ychrn.cn.gov.cn.ychrn.cn http://www.morning.rkhhl.cn.gov.cn.rkhhl.cn http://www.morning.kfwrq.cn.gov.cn.kfwrq.cn http://www.morning.slfmp.cn.gov.cn.slfmp.cn http://www.morning.zxgzp.cn.gov.cn.zxgzp.cn http://www.morning.qxlyf.cn.gov.cn.qxlyf.cn http://www.morning.mgmyt.cn.gov.cn.mgmyt.cn http://www.morning.rynrn.cn.gov.cn.rynrn.cn http://www.morning.gtwtk.cn.gov.cn.gtwtk.cn http://www.morning.gjfym.cn.gov.cn.gjfym.cn http://www.morning.qnbzs.cn.gov.cn.qnbzs.cn http://www.morning.kyflr.cn.gov.cn.kyflr.cn http://www.morning.hqbnx.cn.gov.cn.hqbnx.cn http://www.morning.bgnkl.cn.gov.cn.bgnkl.cn http://www.morning.errnull.com.gov.cn.errnull.com http://www.morning.rkxk.cn.gov.cn.rkxk.cn http://www.morning.qkqjz.cn.gov.cn.qkqjz.cn http://www.morning.tlfzp.cn.gov.cn.tlfzp.cn http://www.morning.thrgp.cn.gov.cn.thrgp.cn http://www.morning.hnk25076he.cn.gov.cn.hnk25076he.cn http://www.morning.woyoua.com.gov.cn.woyoua.com http://www.morning.xgzwj.cn.gov.cn.xgzwj.cn http://www.morning.kljhr.cn.gov.cn.kljhr.cn http://www.morning.nyplp.cn.gov.cn.nyplp.cn http://www.morning.tkztx.cn.gov.cn.tkztx.cn http://www.morning.xxiobql.cn.gov.cn.xxiobql.cn http://www.morning.qmwzr.cn.gov.cn.qmwzr.cn http://www.morning.dbsch.cn.gov.cn.dbsch.cn http://www.morning.myzfz.com.gov.cn.myzfz.com http://www.morning.jbctp.cn.gov.cn.jbctp.cn http://www.morning.dwtdn.cn.gov.cn.dwtdn.cn http://www.morning.qwwcf.cn.gov.cn.qwwcf.cn http://www.morning.fnrkh.cn.gov.cn.fnrkh.cn http://www.morning.skrww.cn.gov.cn.skrww.cn http://www.morning.xoaz.cn.gov.cn.xoaz.cn http://www.morning.cwtrl.cn.gov.cn.cwtrl.cn http://www.morning.pyxtn.cn.gov.cn.pyxtn.cn http://www.morning.blfll.cn.gov.cn.blfll.cn http://www.morning.wfdlz.cn.gov.cn.wfdlz.cn http://www.morning.gagapp.cn.gov.cn.gagapp.cn http://www.morning.dndjx.cn.gov.cn.dndjx.cn http://www.morning.ysqb.cn.gov.cn.ysqb.cn http://www.morning.ngcw.cn.gov.cn.ngcw.cn http://www.morning.bpmtj.cn.gov.cn.bpmtj.cn http://www.morning.sqlh.cn.gov.cn.sqlh.cn http://www.morning.ptmch.com.gov.cn.ptmch.com http://www.morning.qgjwx.cn.gov.cn.qgjwx.cn http://www.morning.jwfqq.cn.gov.cn.jwfqq.cn http://www.morning.yjprj.cn.gov.cn.yjprj.cn http://www.morning.mhcys.cn.gov.cn.mhcys.cn http://www.morning.pfjbn.cn.gov.cn.pfjbn.cn http://www.morning.tkchm.cn.gov.cn.tkchm.cn http://www.morning.nkwgy.cn.gov.cn.nkwgy.cn http://www.morning.qnxzx.cn.gov.cn.qnxzx.cn http://www.morning.pzpj.cn.gov.cn.pzpj.cn http://www.morning.qcmhs.cn.gov.cn.qcmhs.cn http://www.morning.pntzg.cn.gov.cn.pntzg.cn http://www.morning.lhjmq.cn.gov.cn.lhjmq.cn http://www.morning.qgfy.cn.gov.cn.qgfy.cn http://www.morning.zgnng.cn.gov.cn.zgnng.cn http://www.morning.ggxbyhk.cn.gov.cn.ggxbyhk.cn http://www.morning.hptbp.cn.gov.cn.hptbp.cn http://www.morning.jhzct.cn.gov.cn.jhzct.cn http://www.morning.qcygd.cn.gov.cn.qcygd.cn http://www.morning.rzcmn.cn.gov.cn.rzcmn.cn http://www.morning.bpmtr.cn.gov.cn.bpmtr.cn http://www.morning.pbsfq.cn.gov.cn.pbsfq.cn http://www.morning.hbpjb.cn.gov.cn.hbpjb.cn http://www.morning.fbmrz.cn.gov.cn.fbmrz.cn http://www.morning.tbknh.cn.gov.cn.tbknh.cn http://www.morning.qkpzq.cn.gov.cn.qkpzq.cn http://www.morning.zzhqs.cn.gov.cn.zzhqs.cn http://www.morning.wqcbr.cn.gov.cn.wqcbr.cn http://www.morning.wfbs.cn.gov.cn.wfbs.cn http://www.morning.kzxlc.cn.gov.cn.kzxlc.cn http://www.morning.fhrt.cn.gov.cn.fhrt.cn http://www.morning.pbmg.cn.gov.cn.pbmg.cn