代码随想录回溯算法总结

news/2024/10/17 20:21:32/

77.组合

class Solution {List<List<Integer>> res = new ArrayList();Deque<Integer> path = new ArrayDeque();public List<List<Integer>> combine(int n, int k) {combineHelper(n, k, 1);return res;}private void combineHelper(int n, int k, int startIndex) {if (path.size() == k) {res.add(new ArrayList<>(path));return;}for (int i = startIndex; i <= n - (k - path.size()) + 1; i++) {path.addFirst(i);combineHelper(n, k, i + 1);path.removeFirst();}}
}

216.组合总和III

class Solution {List<List<Integer>> res = new ArrayList();Deque<Integer> path = new ArrayDeque();int sum = 0;public List<List<Integer>> combinationSum3(int k, int n) {combinationSum3Helper(k, n, 1);return res;}private void combinationSum3Helper(int k, int n, int startIndex) {if (path.size() == k) {if (sum == n) {res.add(new ArrayList<>(path));}return;}for (int i = startIndex; i <= 9; i++) {if (sum + i > n) break;sum += i;path.addFirst(i);combinationSum3Helper(k, n, i + 1);path.removeFirst();sum -= i;}}
}

39. 组合总和

class Solution {List<List<Integer>> res = new ArrayList();Deque<Integer> path = new ArrayDeque();int sum = 0;public List<List<Integer>> combinationSum(int[] candidates, int target) {if (candidates == null || candidates.length == 0) return res;Arrays.sort(candidates);combinationSumHelper(candidates, target, 0);return res;}private void combinationSumHelper(int[] candidates, int target, int startIndex) {if (sum == target) {res.add(new ArrayList<>(path));return;}for (int i = startIndex; i < candidates.length; i++) {if (sum + candidates[i] > target) break;sum += candidates[i];path.addFirst(candidates[i]);combinationSumHelper(candidates, target, i);path.removeFirst();sum -= candidates[i];}}
}

40.组合总和II

class Solution {List<List<Integer>> res = new ArrayList();Deque<Integer> path = new ArrayDeque();int sum = 0;public List<List<Integer>> combinationSum2(int[] candidates, int target) {if (candidates == null || candidates.length == 0) return res;Arrays.sort(candidates);combinationSum2Helper(candidates, target, 0);return res;}private void combinationSum2Helper(int[] candidates, int target, int startIndex) {if (sum == target) {res.add(new ArrayList<>(path));return;}for (int i = startIndex; i < candidates.length; i++) {if (i > startIndex && candidates[i] == candidates[i - 1]) continue;if (sum + candidates[i] > target) break;sum += candidates[i];path.addFirst(candidates[i]);combinationSum2Helper(candidates, target, i + 1);path.removeFirst();sum -= candidates[i];}}
}

17.电话号码的字母组合

class Solution {List<String> res = new ArrayList();String[] numString = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};StringBuilder builder = new StringBuilder();public List<String> letterCombinations(String digits) {if (digits == null || digits.length() == 0) {return res;}letterCombinationsHelper(digits, 0);return res;}private void letterCombinationsHelper(String digits, int index) {if (index == digits.length()) {res.add(builder.toString());return;}String s = numString[digits.charAt(index) - '0'];for (int i = 0; i < s.length(); i++) {builder.append(s.charAt(i));letterCombinationsHelper(digits, index + 1);builder.deleteCharAt(builder.length() - 1);}}
}

78.子集

class Solution {List<List<Integer>> res = new ArrayList();Deque<Integer> path = new ArrayDeque();public List<List<Integer>> subsets(int[] nums) {subsetsHelper(nums, 0);return res;}private void subsetsHelper(int[] nums, int startIndex) {res.add(new ArrayList<>(path));for (int i = startIndex; i < nums.length; i++) {path.addFirst(nums[i]);subsetsHelper(nums, i + 1);path.removeFirst();}}
}

http://www.ppmy.cn/news/1095864.html

相关文章

70. 爬楼梯 (进阶),322. 零钱兑换,279.完全平方数

代码随想录训练营第45天|70. 爬楼梯 &#xff08;进阶&#xff0c;322. 零钱兑换&#xff0c;279.完全平方数 70.爬楼梯文章思路代码 322.零钱兑换文章思路代码 279.完全平方数文章思路代码 总结 70.爬楼梯 文章 代码随想录|0070.爬楼梯完全背包版本 思路 将楼梯长度视为背…

Sentinel1.8.6集成nacos

代码&#xff1a;https://gitee.com/gsls200808/sentinel-dashboard-nacos jar包&#xff1a;https://gitee.com/gsls200808/sentinel-dashboard-nacos/releases/tag/v1.8.6.0 代码如果看不到可能需要登录。 官方参考文档&#xff1a; 动态规则扩展 alibaba/Sentinel Wiki…

技术面试与HR面:两者之间的关联与区别

&#x1f337;&#x1f341; 博主猫头虎&#xff08;&#x1f405;&#x1f43e;&#xff09;带您 Go to New World✨&#x1f341; &#x1f984; 博客首页——&#x1f405;&#x1f43e;猫头虎的博客&#x1f390; &#x1f433; 《面试题大全专栏》 &#x1f995; 文章图文…

得心应手应对 OOM 的疑难杂症

Java全能学习面试指南&#xff1a;https://www.javaxiaobear.cn/ 前面我们提到&#xff0c;类的初始化发生在类加载阶段&#xff0c;那对象都有哪些创建方式呢&#xff1f;除了我们常用的 new&#xff0c;还有下面这些方式&#xff1a; 使用 Class 的 newInstance 方法。使用…

【大数据之Kafka】九、Kafka Broker之文件存储及高效读写数据

1 文件存储 1.1 文件存储机制 Topic是逻辑上的概念&#xff0c;而partition是物理上的概念&#xff0c;每个partition对应于一个log文件&#xff0c;该log文件中存储的是Producer生产的数据。 Producer生产的数据会被不断追加到该log文件末端&#xff0c;为防止log文件过大导致…

晨启,MSP430开发板,51开发板,原理图,PCB图

下载&#xff1a;https://github.com/xddun/blog_code_search

PHP反序列化漏洞

一、序列化&#xff0c;反序列化 序列化&#xff1a;将php对象压缩并按照一定格式转换成字符串过程反序列化&#xff1a;从字符串转换回php对象的过程目的&#xff1a;为了方便php对象的传输和存储 seriallize() 传入参数为php对象&#xff0c;序列化成字符串 unseriali…

Golang开发--interface的使用

在Go语言中&#xff0c;接口&#xff08;interface&#xff09;是一种特殊的类型&#xff0c;它定义了一组方法的集合。接口为实现多态性提供了一种机制&#xff0c;允许不同的数据类型实现相同的方法&#xff0c;从而可以以统一的方式处理这些不同类型的对象。接口在Go中广泛用…