Algorithm Questions Practice Daily---Day 91: Combination Sum III

Get into the habit of writing together! This is the 26th day of my participation in the "Nuggets Daily New Plan · April Update Challenge", click to view the event details .

1. Problem description

Find all  combinations nof  knumbers that add up and satisfy the following conditions:

  • Use only numbers 1 to 9
  • Use each number  at most once 

Returns  a list of all possible valid combinations  . The list cannot contain the same combination twice, the combinations can be returned in any order.

Topic link: Combined Sum III

Second, the subject requirements

Sample

输入: k = 3, n = 7
输出: [[1,2,4]]
解释:
1 + 2 + 4 = 7
没有其他符合的组合了。
复制代码

visit

1.回溯算法
2.建议用时20~35min
复制代码

3. Problem Analysis

This question is the first 3question to open the backtracking algorithm to brush the question. If you have not understood it before, you can take a look at this question solution and explain it in more detail.

Algorithm questions daily practice---Day 85: Combination

This question is a daily practice with the algorithm question we brushed before ---Day 88: Combination Sum II is very similar, or it is simpler, because this question only allows the use of 1~9, and there will be no repetition elements, do not need to carry out the heavy pruning operation.

The first formula: function initial

We want to initialize a function to carry the backtracking. How to determine the parameters in the function?

First, you need to pass in the given array information, the target value, and the index of the initial traversal.

    vector<int>t;
    vector<vector<int>>v;
void DFS(int begin,int target,int k)//函数初始
复制代码

Type 2: Termination Condition

When to end the condition to continue down and start the search?

The title requires which elements of the array the target can consist of. For each element added, the target must subtract the value of this element.

If target==0, and the number of array elements at this time is equal to k, then the termination condition is met.

if(target==0&&t.size()==k)//终止条件
{
    v.push_back(t);
    return;
}
复制代码

The third formula: pruning optimization

The pruning of this question is relatively simple, and the above termination condition is not equal to 0.

Then if the target is already less than 0 and the number of arrays is greater than k, do we still need to continue searching? It is completely unnecessary.

if(target<0||t.size()>k)//剪枝优化
        return;
复制代码

The fourth type: recursive processing

for(int i=begin;i<=9;i++)//递归处理
{
    if(i<=target)
    {
         t.push_back(i);//添加数据
         DFS(i+1,target-i,k);
         t.pop_back();//回溯
    }
}
复制代码

Each time if i is less than target, add to the array and subtract the value of i from the target value.

Template :

void DFS(变量)//函数初始
{
    if(条件1||条件2...)//终止条件
    {
         v.push_back(t);
         return;
    }
    if (条件1||条件2...)//剪枝
         return;
    for(int i=cur;i<=n;i++)//递归处理
    {
        //选择当前数字
        DFS(向下遍历);
        //回退
    } 
}
复制代码

rush rush!

25.gif

Fourth, the encoding implementation

class Solution {
public:
    vector<int>t;
    vector<vector<int>>v;
    void DFS(int begin,int target,int k)//函数初始
    {
        if(target==0&&t.size()==k)//终止条件
        {
            v.push_back(t);
            return;
        }
        if(target<0||t.size()>k)//剪枝优化
            return;
        for(int i=begin;i<=9;i++)//递归处理
        {
            if(i<=target)
            {
                t.push_back(i);
                DFS(i+1,target-i,k);
                t.pop_back();
            }
        }
    }
    vector<vector<int>> combinationSum3(int k, int n) {  
        DFS(1,n,k);//调用回溯函数
        return v;
    }
};
复制代码

5. Test results

2.png

3.png

Guess you like

Origin juejin.im/post/7090850555815755812