力扣爆刷第123天之回溯五连刷

embedded/2024/9/24 11:29:24/

力扣爆刷第123天之回溯五连刷

文章目录

      • 力扣爆刷第123天之回溯五连刷
      • 一、77. 组合
      • 二、216. 组合总和 III
      • 三、17. 电话号码的字母组合
      • 四、39. 组合总和
      • 五、40. 组合总和 II

一、77. 组合

题目链接:https://leetcode.cn/problems/combinations/description/
思路:元素无重,不可复用,求组合数,可以早停。常规做法套模板。

class Solution {List<List<Integer>> resList = new ArrayList<>();List<Integer> list = new ArrayList<>();public List<List<Integer>> combine(int n, int k) {backTracking(n, k, 1);return resList;}void backTracking(int n, int k, int start) {if(list.size() == k) {resList.add(new ArrayList(list));return;}for(int i = start; i <= n && n - (k - list.size()) + 1 >= i; i++) {list.add(i);backTracking(n, k, i+1);list.remove(list.size()-1);}}
}

二、216. 组合总和 III

题目链接:https://leetcode.cn/problems/combination-sum-iii/description/
思路:元素无重,不可复选,要求和为n的k个数的组合,只需要简单的判断和早停,其他套模板。

class Solution {List<List<Integer>> resList = new ArrayList<>();List<Integer> list = new ArrayList<>();int sum = 0;public List<List<Integer>> combinationSum3(int k, int n) {if(k > n) return resList;backTracking(k, n, 1);return resList;}void backTracking(int k, int n, int start) {if(sum == n && list.size() == k) {resList.add(new ArrayList(list));return;}for(int i = start; i <= 9 && sum + i <= n; i++) {list.add(i);sum += i;backTracking(k, n, i+1);sum -= i;list.remove(list.size()-1);}}}

三、17. 电话号码的字母组合

题目链接:https://leetcode.cn/problems/letter-combinations-of-a-phone-number/description/
思路:集合无重,元素不可复用,本题不同的点是所使用的集合是可以变化的,每次向下递归需要更换下一个集合。

class Solution {List<String> list = new ArrayList<>();StringBuilder builder = new StringBuilder();String[] source = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};public List<String> letterCombinations(String digits) {if(digits.equals("")) return list;backTracking(0, digits);return list;}void backTracking(int start, String digits) {if(builder.length() == digits.length()) {list.add(builder.toString());return;}String temp = source[digits.charAt(start) - '0'];for(int i = 0; i < temp.length(); i++) {builder.append(temp.charAt(i));backTracking(start+1, digits);builder.deleteCharAt(builder.length()-1);}}
}

四、39. 组合总和

题目链接:https://leetcode.cn/problems/combination-sum/description/
思路:集合无重,元素可复选,求和为K的组合数,向下递归索引位置为i不用加1,确保单个元素可以复选,排序后可以早停。其他套模板。

class Solution {List<List<Integer>> resList = new ArrayList<>();List<Integer> list = new ArrayList<>();int sum = 0;public List<List<Integer>> combinationSum(int[] candidates, int target) {Arrays.sort(candidates);backTracking(candidates, target, 0);return resList;}void backTracking(int[] candidates, int target, int start) {if(sum == target) {resList.add(new ArrayList(list));return;}for(int i = start; i < candidates.length && sum + candidates[i] <= target; i++) {sum += candidates[i];list.add(candidates[i]);backTracking(candidates, target, i);sum -= candidates[i];list.remove(list.size()-1);}}}

五、40. 组合总和 II

题目链接:https://leetcode.cn/problems/combination-sum-ii/description/
思路:集合元素有重,不可复选,求和为K的组合数,数组和排序,排序后,纵向不去重,横向去重,横向只需要大于初始索引,前后两个值想对,即去重。其他一样。

class Solution {List<List<Integer>> resList = new ArrayList<>();List<Integer> list = new ArrayList<>();int sum = 0;public List<List<Integer>> combinationSum2(int[] candidates, int target) {Arrays.sort(candidates);backTracking(candidates, target, 0);return resList;}void backTracking(int[] candidates, int target, int start) {if(sum == target) {resList.add(new ArrayList(list));return;}for(int i = start; i < candidates.length && sum + candidates[i] <= target; i++) {if(i > start && candidates[i] == candidates[i-1]) continue;sum += candidates[i];list.add(candidates[i]);backTracking(candidates, target, i+1);sum -= candidates[i];list.remove(list.size()-1);}}}

http://www.ppmy.cn/embedded/2664.html

相关文章

响应式修改 页面字体字号【大 中 小 】

浅浅记录下&#xff0c;工作中遇到的问题&#xff0c;修改页面文本字号。 <p class"change_fontSize">[ 字号 <a href"javascript:doZoom(18)">大</a><a href"javascript:doZoom(16)">中</a><a href"ja…

docker快速使用简介

进入服务器 ssh root192.0.0.211安装 docker load < bevformer_image.tar修改镜像的REPOSITORY和TAG docker tag a6a4c15ca9db bevformer:1.0其中&#xff0c;a6a4c15ca9db是原来镜像的id。bevformer是修改后的REPOSITORY&#xff1b;1.0是修改后的TAG。 从Docker Hub上…

【C语言】指针详解(五)

目录 1.字符指针 1.1常量字符串 2.指针数组 3.数组指针 1.字符指针 字符指针就是指向字符的指针&#xff0c;字符指针可以存储字符变量的地址。 举例如下&#xff1a; 定义指针变量pa存a的地址&#xff0c;改变*pa的值&#xff0c;a也会随之改变 。 1.1常量字符串 &#x1f…

selenium——chromdriver版本请及时更新

接python学习笔记&#xff08;23&#xff09;内容 如果某个时刻&#xff0c;你突然发现selenium功能出错了&#xff0c;有可能是google升级了 我们再看看我们现在的google版本 右上角三点>>帮助>>关于chrome>>版本 124.0.6367.61 很显然已经升级了&#…

【C++】每日一题 48 旋转图像

给定一个 n n 的二维矩阵 matrix 表示一个图像。请你将图像顺时针旋转 90 度。 你必须在 原地 旋转图像&#xff0c;这意味着你需要直接修改输入的二维矩阵。请不要 使用另一个矩阵来旋转图像。 #include <vector> using namespace std;class Solution { public:void …

【Tesla T4为例】GPU安装最新版本NVIDIA Driver、CUDA、cuDNN、Anaconda、Pytorch

NVIDIA Driver 进入英伟达官网下载页面 按照以上方式选择即可得到>535.113.01版本的驱动&#xff0c;可以实现多卡推理&#xff0c;小于这个版本会导致多卡训练以及推理报错 虽然最新版本为550.54.15&#xff0c;但是535版本更加稳定&#xff0c;并且pytorch目前只支持到1…

【鸿蒙开发】生命周期

1. UIAbility组件生命周期 UIAbility的生命周期包括Create、Foreground、Background、Destroy四个状态。 UIAbility生命周期状态 1.1 Create状态 Create状态为在应用加载过程中&#xff0c;UIAbility实例创建完成时触发&#xff0c;系统会调用onCreate()回调。可以在该回调中…

Mybatis generate xml 没有被覆盖

添加插件即可 <plugin type"org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin"/>