当前位置: 首页 > news >正文

Combination Sum系列问题

主要使用方法是backtracking。

Combination Sum

Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.

The same repeated number may be chosen from C unlimited number of times.

Note:

  • All numbers (including target) will be positive integers.
  • Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
  • The solution set must not contain duplicate combinations.

 

For example, given candidate set 2,3,6,7 and target 7
A solution set is: 
[7] 
[2, 2, 3] 

解答:为了减少许多不必要的循环过程,应该先把candidates排序,这样当目前遍历到的元素和已经大于target时,就可以不必再访问candidates后面的元素,直接退回到上个选择处进行选择。

另外,由于candidates集合中的每个数都可以使用无数次,故每次递归调用都应该从上次加入的元素开始遍历。

public class Solution {
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        List<List<Integer>> result = new LinkedList<List<Integer>>();
        if (candidates == null || candidates.length == 0) {
            return result;
        }
        Arrays.sort(candidates);
        List<Integer> temp = new LinkedList<Integer>();
        helper(candidates, target, result, temp, 0);
        return result;
    }
    public void helper (int[] candidates, int target, List<List<Integer>> result, List<Integer> temp, int index) {
        if (target == 0) {
            result.add(new LinkedList<Integer>(temp));
            return;
        }
        for (int i = index; i < candidates.length && candidates[i] <= target; i++) {
            temp.add(candidates[i]);
            helper(candidates, target - candidates[i], result, temp, i);  //从上次加入的元素开始遍历
            temp.remove(temp.size() - 1);
        }
    }
}

Combination Sum II

Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.

Each number in C may only be used once in the combination.

Note:

  • All numbers (including target) will be positive integers.
  • Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
  • The solution set must not contain duplicate combinations.

 

For example, given candidate set 10,1,2,7,6,1,5 and target 8
A solution set is: 
[1, 7] 
[1, 2, 5] 
[2, 6] 
[1, 1, 6] 

解答:与上题一样应该先把candidates排序。

这题需要注意的有两点是:

1. 每个元素只能用一次,所以每次递归都应该从当前加入元素的下一个元素开始遍历;

2. 集合中含有重复元素,所以在每次for循环在candidates挑选元素时,应将已经挑选过的元素过滤(因为加入此元素的结果已经加入结果集合),避免集合中结果出现重复。递归中则不用考虑起始点与上个元素是否相同,因为递归是在前面元素确定的情况下加入下一元素,它们在同一结果中。

public class Solution {
    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        Arrays.sort(candidates);
        List<List<Integer>> rst = new ArrayList<List<Integer>>();
        List<Integer> temp = new ArrayList<Integer>();
        helper(rst, temp, candidates, target, 0);
        return rst;
    }
    public void helper(List<List<Integer>> rst, List<Integer> temp, int[] candidates, int target, int index) {
        if (target == 0) {
            rst.add(new ArrayList<Integer>(temp));
            return;
        }
        for (int i = index; i < candidates.length && candidates[i] <= target; i++) {
            if (i > index && candidates[i] == candidates[i - 1]) {
                continue;
            }
            temp.add(candidates[i]);
            helper(rst, temp, candidates, target - candidates[i], i + 1);  //从下一个元素开始遍历
            temp.remove(temp.size() - 1);
        }
    }
}

Combination Sum III

Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers.

Ensure that numbers within the set are sorted in ascending order.

Example 1:

Input: k = 3, n = 7

Output:

[[1,2,4]]

Example 2:

Input: k = 3, n = 9

Output:

[[1,2,6], [1,3,5], [2,3,4]]

解答:可以看做是上题的一种特殊情况, candidates数组中的元素为1到9,且不含重复元素,上题中去重的判断可以去掉。

public class Solution {
    public List<List<Integer>> combinationSum3(int k, int n) {
        List<List<Integer>> rst = new ArrayList<List<Integer>>();
        List<Integer> temp = new ArrayList<Integer>();
        helper(rst, temp, k, n, 1);
        return rst;
    }
    public void helper(List<List<Integer>> rst, List<Integer> temp, int k, int n, int number) {
        if (k == temp.size() && n == 0) {
            rst.add(new ArrayList<Integer>(temp));
            return;
        }
        for (int i = number; i <= 9 && i <= n; i++) {
            temp.add(i);
            helper(rst, temp, k, n - i, i + 1);
            temp.remove(temp.size() - 1);
        }
    }
}

总结:本题需要注意的是每次递归里面的循环起始点,以及如何避免结果集的重复。



转载于:https://www.cnblogs.com/shinning/p/5492503.html

相关文章:

  • js中容易被忽视的事件问题总结
  • Web Service 接口安全与解决方案
  • B树、B-树、B+树、B*树的定义和区分
  • 史上最全大数据学习资源整理(1)
  • Hive操作表部分总结
  • 电邮欺诈需重视 TurboMail邮件系统保护您
  • IOS-利用AFNetworking监听网络状态
  • WCF学习之旅—WCF服务部署到应用程序(十)
  • 第三节课作业——指针
  • AngularJS 应用身份认证的技巧
  • UDP数据报
  • 实时预测用户对物品偏好 阿里云推荐引擎帮助你更好的提升业务
  • PHPer书单
  • 【译】使用newInstance()来实例化fragment
  • Android 2.3 r1 中文API (78)—— ViewAnimator
  • 自己简单写的 事件订阅机制
  • axios 和 cookie 的那些事
  • cookie和session
  • java小心机(3)| 浅析finalize()
  • JS实现简单的MVC模式开发小游戏
  • js数组之filter
  • leetcode386. Lexicographical Numbers
  • Netty 4.1 源代码学习:线程模型
  • node和express搭建代理服务器(源码)
  • PHP的Ev教程三(Periodic watcher)
  • Redis字符串类型内部编码剖析
  • Vue源码解析(二)Vue的双向绑定讲解及实现
  • 复习Javascript专题(四):js中的深浅拷贝
  • 基于游标的分页接口实现
  • 免费小说阅读小程序
  • 如何胜任知名企业的商业数据分析师?
  • 小试R空间处理新库sf
  • 自动记录MySQL慢查询快照脚本
  • 3月27日云栖精选夜读 | 从 “城市大脑”实践,瞭望未来城市源起 ...
  • ###51单片机学习(1)-----单片机烧录软件的使用,以及如何建立一个工程项目
  • (delphi11最新学习资料) Object Pascal 学习笔记---第7章第3节(封装和窗体)
  • (rabbitmq的高级特性)消息可靠性
  • (附源码)springboot 校园学生兼职系统 毕业设计 742122
  • (十八)用JAVA编写MP3解码器——迷你播放器
  • (一)Thymeleaf用法——Thymeleaf简介
  • (转)重识new
  • .mysql secret在哪_MYSQL基本操作(上)
  • .NET “底层”异步编程模式——异步编程模型(Asynchronous Programming Model,APM)...
  • .net 生成二级域名
  • .NET/C# 获取一个正在运行的进程的命令行参数
  • .NET:自动将请求参数绑定到ASPX、ASHX和MVC(菜鸟必看)
  • .net6Api后台+uniapp导出Excel
  • .Net6支持的操作系统版本(.net8已来,你还在用.netframework4.5吗)
  • .NET建议使用的大小写命名原则
  • .net中调用windows performance记录性能信息
  • .py文件应该怎样打开?
  • /dev下添加设备节点的方法步骤(通过device_create)
  • @Bean, @Component, @Configuration简析
  • @configuration注解_2w字长文给你讲透了配置类为什么要添加 @Configuration注解
  • @DateTimeFormat 和 @JsonFormat 注解详解