十七
十七
Published on 2022-03-03 / 179 Visits
0
0

leetcode216. 组合总和 III

leetcode216. 组合总和 III

dfs

class Solution {
public:
    vector<vector<int>> ans;
    vector<int> path;
    void dfs(int cur,int u,int k,int target){
        if(u>k)return;
        if(target==0&&u==k){
            ans.push_back(path);
            return;
        }
        for(int i=cur;i<=9;i++){
            if(i<=target){
                path.push_back(i);
                dfs(i+1,u+1,k,target-i);
                path.pop_back();
            }
        }
    }
    vector<vector<int>> combinationSum3(int k, int n) {
        dfs(1,0,k,n);
        return ans;
    }
};

Comment