Leetcode216-组合总和三

  • 找出所有相加之和为 n 的 k 个数的组合,且满足下列条件:
  • 只使用数字1到9
  • 每个数字 最多使用一次
  • 返回 所有可能的有效组合的列表 。该列表不能包含相同的组合两次,组合可以以任何顺序返回
  • 输入: k = 3, n = 7
  • 输出: [[1,2,4]]
  ArrayList<List<Integer>> res=new ArrayList<>();
LinkedList<Integer> integers=new LinkedList<>();
int sum=0;
public List<List<Integer>> combinationSum3(int k, int n) {
combine(k,n,1);
return res;
} public void combine(int k,int n,int startIndex) {
if (sum != n && integers.size() == k) {
return;
}
if (sum == n && integers.size() == k) {
res.add(new LinkedList<Integer>(integers));
}
for (int i = startIndex; i <= 9; i++) {
sum += i;
integers.addFirst(i);
//使用addFirst和removeFirst每个integers里面是倒序的
//要正序使用add和removeLast就行了
combine(k, n, i + 1);
integers.removeFirst();
sum -= i;
}
}

Leetcode39-组合总和

  • 给你一个 无重复元素 的整数数组 candidates 和一个目标整数 target ,找出 candidates 中可以使数字和为目标数 target 的 所有 不同组合 ,并以列表形式返回。你可以按 任意顺序 返回这些组合。
  • candidates 中的 同一个 数字可以 无限制重复被选取 。如果至少一个数字的被选数量不同,则两种组合是不同的。
  • 输入:candidates = [2,3,6,7], target = 7
  • 输出:[[2,2,3],[7]]
    ArrayList<List<Integer>> res = new ArrayList<>();
LinkedList<Integer> integers = new LinkedList<>();
int sum = 0; public List<List<Integer>> combinationSum(int[] candidates, int target) {
combine(candidates, target, 0);
return res;
} public void combine(int[] candidates, int target, int startIndex) {
if (sum > target) {
return;
}
if (sum == target) {
res.add(new LinkedList<>(integers));
return;
}
for (int i = startIndex; i < candidates.length; i++) {
sum += candidates[i];
integers.add(candidates[i]);
combine(candidates, target, i);
integers.removeLast();
sum -= candidates[i];
}
}

Leetcode40-组合总和二

  • 给定一个候选人编号的集合 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。
  • candidates 中的每个数字在每个组合中只能使用 一次
  • 注意:解集不能包含重复的组合。
  • 输入: candidates = [10,1,2,7,6,1,5], target = 8,
  • 输出:
    [
    [1,1,6],
    [1,2,5],
    [1,7],
    [2,6]
    ]
  ArrayList<List<Integer>> res = new ArrayList<>();
LinkedList<Integer> integers = new LinkedList<>();
int sum = 0; public List<List<Integer>> combinationSum2(int[] candidates, int target) {
Arrays.sort(candidates);
//加标志数组,用来辅助判断同层节点是否已经遍历
boolean[] flag = new boolean[candidates.length];
combine(candidates, target, 0, flag);
return res;
} public void combine(int[] candidates, int target, int startIndex, boolean[] flag) {
if (sum > target) {
return;
}
if (sum == target) {
res.add(new LinkedList<>(integers));
return;
}
for (int i = startIndex; i < candidates.length; i++) {
//出现重复节点直接跳过
if (i > 0 && candidates[i] == candidates[i - 1] && flag[i - 1] == false) {
continue;
}
flag[i] = true;
sum += candidates[i];
integers.add(candidates[i]);
combine(candidates, target, i + 1, flag);
integers.removeLast();
sum -= candidates[i];
flag[i] = false;
}
}

L77-组合

  • 给定两个整数 n 和 k,返回范围 [1, n] 中所有可能的 k 个数的组合。
  • 你可以按 任何顺序 返回答案
  • 输入:n = 4, k = 2
  • 输出:
    [
    [2,4],
    [3,4],
    [2,3],
    [1,2],
    [1,3],
    [1,4],
    ]
    List<List<Integer>> result = new ArrayList<>();
LinkedList<Integer> path = new LinkedList<>();
public List<List<Integer>> combine(int n, int k) {
combineRes(n,k,1);
return result;
} public void combineRes(int n,int k,int startIndex){
//终止条件
if(path.size()==k){
result.add(new ArrayList<>(path));//防止递归操作影响以及保存好的path
return;
}
//剪枝优化 此处仔细想想
//如果for循环选择的起始位置之后的元素个数 已经不足 我们需要的元素个数了,那么就没有必要搜索了。
//已经选择的元素个数:path.size();
//还需要的元素个数为: k - path.size();
//在集合n中至多要从该起始位置 : n - (k - path.size()) + 1,开始遍历
//不剪枝直接 for(int i=startIndex;i<=n;i++)
for(int i=startIndex;i<=n-(k-path.size())+1;i++){
path.addFirst(i);
combineRes(n,k,i+1);
path.removeFirst();
}
}

Leetcode216/39/40/77之回溯解决经典组合问题的更多相关文章

  1. python实例:解决经典扑克牌游戏 -- 四张牌凑24点 (二)

    Hey! 如果你还没有看这篇的上文的话,可以去稍稍瞅一眼,会帮助加速理解这一篇里面涉及到的递归结构哦!(上一篇点这里:<python实例:解决经典扑克牌游戏 -- 四张牌凑24点 (一)> ...

  2. 三十五、lamp经典组合搭建

    一.安装mysql数据库 1.1  创建组和用户: 1)groupadd mysql 2)useradd mysql   -g mysql -M  -s  /sbin/nologin 3)config ...

  3. python实例:解决经典扑克牌游戏 -- 四张牌凑24点 (一)

    Hey! Hope you had a great day so far! 今天想和大家讨论的是一道我从这学期cs的期末考试得到灵感的题:Get 24 Poker Game.说到 Get 24 Pok ...

  4. 39. Combination Sum(medium, backtrack 的经典应用, 重要)

    Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique c ...

  5. <BackTracking> dfs: 39 40

    39. Combination Sum Combination,组合问题用DFS罗列全部的答案. class Solution { public List<List<Integer> ...

  6. leetcode四道组合总和问题总结(39+40+216+377)

    39题目: 链接:https://leetcode-cn.com/problems/combination-sum/ 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 ...

  7. Leetcode 39 40 216 Combination Sum I II III

    Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...

  8. 77. Combinations(回溯)

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. Example: I ...

  9. c++回溯法求组合问题(取数,选取问题)从n个元素中选出m个的回溯算法

    假如现在有n个数,分别从里面选择m个出来,那么一共有多少种不同的组合呢,分别是哪些呢? 利用计算机的计算力,采用回溯算法很容易求解 程序源代码如下: #include<iostream># ...

随机推荐

  1. python-用代码实现队列,处理斐波那契数列

    队列在进行数据操作时必须遵循"先进先出(Firstin Firstout,FIFO)"的原则,这一特点决定了队列的基本操作需要在其两端进行 队列(Queue)的基本操作通常在队列的 ...

  2. windows安装oos遇到的坑 (汇总)

    一.环境安装 --  搭建域控服务器 1. 打开服务器管理器,添加角色和功能: 2.下一步: 3.下一步: 4.下一步 5.选择添加AD域服务,同时添加所需功能,下一步: 6.安装功能,下一步: 7. ...

  3. Vue中图片的加载方式

    一.前言 VUE项目中图片的加载是必须的,那么vue中图片的加载方式有哪些呢,今天博主就抽点时间来为大家大概地捋一捋. 二.图片的加载方法 1.在本地加载图片(静态加载) 图片存放assets文件夹中 ...

  4. [WPF] 假装可变字体

    1. 可变字体 上图中的两个动画,一个文字直接变粗,一个渐渐变粗,我觉得后者会更有趣.但普通的字体可达不到这种效果,例如微软雅黑,无论怎么调整它的 FontWeight,实际上它也只有三种粗细: 这时 ...

  5. 关于Gradle 6.x及以上版本发布到仓库有很多CheckSum文件,想去除?

    写在前边 今天写的这个博客和平时的博客有点区别,大多数都是告诉你解决问题的,而本文不完全是. 经常使用Gradle的都知道 Gradle有两个发布制品到Maven仓库的插件:maven 与 maven ...

  6. Mybatis 的 Xml 映射文件中,不同的 Xml 映射文件,id 是否可以重复?

    不同的 Xml 映射文件,如果配置了 namespace,那么 id 可以重复:如果没有配 置 namespace,那么 id 不能重复: 原因就是 namespace+id 是作为 Map<S ...

  7. jvm-learning-类加载器分类

    public class ClassLoaderTest { public static void main(String[] args) { //获取系统类加载器 ClassLoader syste ...

  8. java线程池源码分析

    我们在关闭线程池的时候会使用shutdown()和shutdownNow(),那么问题来了: 这两个方法又什么区别呢? 他们背后的原理是什么呢? 线程池中线程超过了coresize后会怎么操作呢? 为 ...

  9. 什么是 Idempotence 以及它在哪里使用?

    幂等性是能够以这样的方式做两次事情的特性,即最终结果将保持不变,即好像 它只做了一次. 用法:在远程服务或数据源中使用 Idempotence,这样当它多次接收指令时,它 只处理指令一次.

  10. pip 安装更新卸载 pip/yum换源

    pip安装:sudo apt-get install python3-pip pip更新:sudo pip3 install --upgrade pip pip卸载:sudo apt-get remo ...