216. 组合总和 III
class Solution {
private:
vector<vector<int>> result;
vector<int> path;
void backtracking(int n,int sum,int k,int startIndex){
if(path.size()==k){
if(sum==n){
result.push_back(path);
return;
}
}
for(int i=startIndex;i<=9;i++){
sum+=i;
path.push_back(i);
backtracking(n,sum,k,i+1);
sum-=i;
path.pop_back();
}
}
public:
vector<vector<int>> combinationSum3(int k, int n) {
backtracking(n,0,k,1);
return result;
}
};
17. 电话号码的字母组合
class Solution {
public:
string letterMaps[10]={
"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"
};
private:
vector<string> result;
string s;
void backtracking(string& digits,int startIndex){
if(startIndex==digits.size()){
result.push_back(s);
return;
}
int n=digits[startIndex]-'0';
string str=letterMaps[n];
for(int i=0;i<str.size();i++){
s.push_back(str[i]);
backtracking(digits,startIndex+1);
s.pop_back();
}
}
public:
vector<string> letterCombinations(string digits) {
if(digits.size()==0) return result;
backtracking(digits,0);
return result;
}
};