news 2026/4/15 20:31:42

力扣--回溯篇(1)

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
力扣--回溯篇(1)

回溯

1.理论基础

递归下面就是回溯。

回溯搜索法,其实是一个纯暴力搜索。

回溯解决的问题:组合问题,切割问题,子集问题,排列问题,棋盘问题

递归函数没有返回值,终止条件+单层搜索逻辑(for循环处理集合元素)+回溯

void backtracking(/*参数*/) { if (/*终止条件*/) { //存放结果; return; } for (/*选择:本层集合中元素(树中节点孩子的数量就是集合的大小)*/) { //处理节点; //backtracking(路径,选择列表); // 递归 //回溯,撤销处理结果 } }

2.组合问题77. 组合 - 力扣(LeetCode)

递归函数参数 返回值

终止条件

单层递归逻辑

classSolution{List<List<Integer>>ans=newArrayList<>();voidbackTrace(List<Integer>tmp,inti,intn,intk){if(tmp.size()==k)//终止条件{ans.add(newArrayList<>(tmp));return;}for(intj=i;j<=n;j++){tmp.add(j);backTrace(tmp,j+1,n,k);tmp.remove(tmp.size()-1);}}publicList<List<Integer>>combine(intn,intk){//其实就是从大到小 然后每次pop 再进他后面的List<Integer>tmp=newArrayList<>();backTrace(tmp,1,n,k);returnans;}}

3.组合问题的剪枝操作

上面那个题目可以进行剪枝,这样往下的深度就不会那么多了。这样就是一个树里可以走到所有值

j<=n-(k-tmp.size())+1

*4.组合总和216. 组合总和 III - 力扣(LeetCode)

classSolution{List<List<Integer>>ans=newArrayList<>();voidbacktracing(List<Integer>tmp,inti,intk,intn){if(tmp.size()==k){if(n==0)ans.add(newArrayList<>(tmp));//不管是不是想要的值都要进行返回了 不然不会popreturn;}//进行剪枝for(intj=i;j<10&&j<=n;j++){tmp.add(j);backtracing(tmp,j+1,k,n-j);tmp.remove(tmp.size()-1);}}publicList<List<Integer>>combinationSum3(intk,intn){List<Integer>tmp=newArrayList<>();backtracing(tmp,1,k,n);returnans;}}

5.电话号码的字母组合17. 电话号码的字母组合 - 力扣(LeetCode)

classSolution{//模板privatestaticfinalString[]MAPPING=newString[]{"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};privatevoiddfs(List<String>ans,inti,char[]digits,char[]path){if(i==digits.length){//到达终止条件ans.add(newString(path));return;}Stringletters=MAPPING[digits[i]-'0'];for(charc:letters.toCharArray()){path[i]=c;dfs(ans,i+1,digits,path);//往后继续递归}}publicList<String>letterCombinations(Stringdigits){intn=digits.length();if(n==0){returnList.of();//直接返回空}List<String>ans=newArrayList<>();//看一下是怎么声明char数组的char[]path=newchar[n];dfs(ans,0,digits.toCharArray(),path);returnans;}}

*6.组合总和39. 组合总和 - 力扣(LeetCode)

相较于之前,元素可以重复使用,所以剩余集合就是可以一直往下。但是要排除掉大于的东西。

classSolution{List<List<Integer>>ans;List<Integer>path;publicvoiddfs(inti,inttarget,int[]candidates){if(i==candidates.length){return;//现在已经全部弄完了}if(target==0){//已经到达最终结果了ans.add(newArrayList(path));return;}//可以直接不选dfs(i+1,target,candidates);//剪枝进行选择if(target-candidates[i]>=0){path.add(candidates[i]);//因为可以重复选择 所以还是从当前元素开始dfs(i,target-candidates[i],candidates);path.remove(path.size()-1);//回退}}publicList<List<Integer>>combinationSum(int[]candidates,inttarget){this.ans=newArrayList<>();this.path=newArrayList<>();dfs(0,target,candidates);returnans;}}

*7.组合总和240. 组合总和 II - 力扣(LeetCode)

classSolution{List<List<Integer>>ans=newArrayList<>();List<Integer>path=newArrayList<>();//只能出现一次 所以每次只能往后加publicvoiddfs(inti,inttarget,int[]candidates){if(target==0){ans.add(newArrayList<>(path));return;}//开始往后进行for(intj=i;j<candidates.length;j++){//剪枝if(target-candidates[j]>=0){System.out.println(i);//排除掉重复的部分if(j>i&&candidates[j]==candidates[j-1]){continue;}//可以继续往下进行操作path.add(candidates[j]);dfs(j+1,target-candidates[j],candidates);path.remove(path.size()-1);}}}publicList<List<Integer>>combinationSum2(int[]candidates,inttarget){Arrays.sort(candidates);dfs(0,target,candidates);returnans;}}

*8.分割回文串131. 分割回文串 - 力扣(LeetCode)

先判断前面的对不对 再往后继续加

classSolution{List<List<String>>ans=newArrayList<>();List<String>path=newArrayList<>();publicbooleanisP(Strings,inta,intb){while(a<b){if(s.charAt(a++)!=s.charAt(b--)){returnfalse;}}returntrue;}publicvoidbackTracing(inti,Strings){//切割完成if(i==s.length()){ans.add(newArrayList<>(path));return;}for(intj=i;j<s.length();j++){if(isP(s,i,j)){path.add(s.substring(i,j+1));//分割backTracing(j+1,s);path.removeLast();}}}publicList<List<String>>partition(Strings){backTracing(0,s);returnans;}}

9.复原IP地址93. 复原 IP 地址 - 力扣(LeetCode)

classSolution{//其实就是分割成四个有效的数字List<String>ans=newArrayList<>();//记录数的集合List<List<Integer>>res=newArrayList<>();List<Integer>path=newArrayList<>();//判断加上当前数是否能组成新的合法数publicbooleanisValid(Strings,intstart,intend){intlen=end-start+1;if(len<1||len>3){returnfalse;}//第一个数不能为0if(len>1&&s.charAt(start)=='0'){returnfalse;}intnum=Integer.parseInt(s.substring(start,end+1));returnnum<=255;}publicvoidbackTracing(inti,Strings){if(path.size()==4){if(i==s.length()){res.add(newArrayList<>(path));}return;}if(s.length()-i>(4-path.size())*3)//剩余字符过多{return;}if(s.length()-i<(4-path.size()))//过少{return;}for(intj=i;j<s.length()&&j<i+3;j++){//继续叠加if(isValid(s,i,j)){//parseIntintnum=Integer.parseInt(s.substring(i,j+1));path.add(num);backTracing(j+1,s);path.removeLast();}}}publicList<String>restoreIpAddresses(Strings){if(s.length()<4||s.length()>12){returnans;}backTracing(0,s);for(List<Integer>nums:res){//里面还是一个数的数组StringBuildersb=newStringBuilder();for(inti=0;i<4;i++){sb.append(nums.get(i));// 每两个元素之间加 "."(最后一个元素后不加)if(i!=nums.size()-1){sb.append(".");}}ans.add(sb.toString());}returnans;}}

10.子集78. 子集 - 力扣(LeetCode)

classSolution{List<List<Integer>>ans=newArrayList<>();List<Integer>path=newArrayList<>();publicvoidbackTracing(intidx,int[]nums){ans.add(newArrayList<>(path));//不管怎么样上来就先直接放进去for(inti=idx;i<nums.length;i++){path.add(nums[i]);backTracing(i+1,nums);path.remove(path.size()-1);}}publicList<List<Integer>>subsets(int[]nums){backTracing(0,nums);returnans;}}

该死的回溯…

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/16 12:16:25

music-api:快速上手的跨平台音乐解析完整教程

还在为不同音乐平台的API接入而烦恼吗&#xff1f;music-api项目为你提供了一站式解决方案&#xff0c;让你轻松获取四大主流音乐平台的歌曲播放地址。无论是开发音乐播放器、构建推荐系统&#xff0c;还是创建个人音乐网站&#xff0c;这个开源工具都能显著提升你的开发效率。…

作者头像 李华
网站建设 2026/4/16 12:13:21

科技赋能文化养老,红松小课开启退休生活品质新时代

在数字中国建设全面推进的今天&#xff0c;科技应用正在为各个年龄群体创造更加便捷、丰富的生活方式。对于规模日益庞大的退休群体而言&#xff0c;科技不仅是工具&#xff0c;更是连接社会、实现自我价值的重要桥梁。专注于服务退休人群的红松小课&#xff0c;正是通过技术创…

作者头像 李华
网站建设 2026/4/16 12:15:09

在asp.net web应用程序,老是访问同一个Handler1.ashx

前言项目是在vs2022下开发&#xff0c;在一般处理程序有二个文件moveToUp.ashx和moveToTop.ashx。在模拟和项目中请求访问时老是指向moveToTop.ashx。上下项目全部度找问题&#xff0c;清仓项目&#xff0c;重新生成&#xff0c;删除bin和obj目录下所有文件&#xff0c;还是这指…

作者头像 李华
网站建设 2026/4/16 4:20:24

机械故障诊断完整指南:基于振动信号分析的实战教程

机械故障诊断完整指南&#xff1a;基于振动信号分析的实战教程 【免费下载链接】机械故障诊断与振动信号数据集 本仓库提供了一个振动信号数据集&#xff0c;旨在帮助工程师和科学家对机械设备的振动信号进行分析和处理。该数据集包含了多个振动信号示例&#xff0c;适用于故障…

作者头像 李华
网站建设 2026/4/16 6:00:17

TIOBE 编程社区 查看各种编程语言流行程度和趋势的社区

TIOBE 编程社区&#xff1a;查看编程语言流行程度和趋势的权威平台 TIOBE 编程社区指数&#xff08;TIOBE Programming Community Index&#xff09;是一个全球知名的编程语言流行度指标&#xff0c;由荷兰软件公司 TIOBE 维护。它通过分析搜索引擎&#xff08;如 Google、Bin…

作者头像 李华