• 组合总和


    Leetcode题目描述

    给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。

    candidates 中的数字可以无限制重复被选取。

    说明:

    所有数字(包括 target)都是正整数。
    解集不能包含重复的组合。
    示例 1:

    输入:candidates = [2,3,6,7], target = 7,
    所求解集为:
    [
      [7],
      [2,2,3]
    ]
    示例 2:
    
    输入:candidates = [2,3,5], target = 8,
    所求解集为:
    [
      [2,2,2,2],
      [2,3,3],
      [3,5]
    ]
    

    提示:

    1 <= candidates.length <= 30
    1 <= candidates[i] <= 200
    candidate 中的每个元素都是独一无二的。
    1 <= target <= 500

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/combination-sum
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    回溯Demo

    • 去重的最好方式在于优先使用最小的数字进行排序,然后下一次添加并且判断永远是第二小的数值
    class Solution{
        public List<List<Integer>> combinationSum(int[] candidates, int target) {
            List<List<Integer>> res = new ArrayList<>();
            backtrack(0, target, candidates, res, new ArrayList<Integer>());
            return res;
        }
        public void backtrack(int s, int t, int[] candidates, List<List<Integer>> res, List<Integer> tmp){
    
            if ( t == 0 ){
                res.add(new ArrayList<Integer>(tmp));
                return;
            }
    
            // 优先使用最小的数字进行排序
            for(; s < candidates.length; s++){
                if( t - candidates[s] >= 0){
                    tmp.add(candidates[s]);
                    backtrack(s, t - candidates[s], candidates, res, tmp);
                    tmp.remove(tmp.size() - 1);
                }
            }
    
        }
    }
    

    回溯demo 部分正确

    有部分的结果冗余

    class Solution {
        public List<List<Integer>> combinationSum(int[] candidates, int target) {
            List<Integer> tmp = new ArrayList<Integer>();
            List<List<Integer>> res = new ArrayList<>();
            int n = candidates.length;
    
    
            backtrack(0, target, candidates, tmp, res);
            return res;
        }
    
        public void backtrack(int now, int target, int[] candidates, List<Integer> tmp, List<List<Integer>> res){
            int n = candidates.length;
            if ( now == target ){
                res.add(new ArrayList<Integer>(tmp));
                return;
            }
            if (now > target) return;
    
            for(int i = 0; i < n; i++){
                now += candidates[i];
                tmp.add(candidates[i]);
                backtrack(now, target, candidates, tmp, res);
                tmp.remove(tmp.size() - 1);
                now = now - candidates[i];
            }
        }
    
        // 第一次BUG, 没有计算重新出现的次数, 这是按照全排列的方式进行的,所以解急会出现冗余
    }
    
  • 相关阅读:
    iOS开发UITableViewCell的选中时的颜色设置
    NSUserDefaults存储数据
    iOS持久化存储
    ios nil、NULL和NSNull 的使用
    IOS NavigationController Toolbar学习笔记
    一、利用NSUserDefaults进行对短小数据的存和取(自定义对象的存取)。
    使用NSUserDefaults保存用户名和密码
    const与static的区别
    CALayer
    UILocalNotification
  • 原文地址:https://www.cnblogs.com/Di-iD/p/13784977.html
Copyright © 2020-2023  润新知