216.组合总和III
![](https://i-blog.csdnimg.cn/blog_migrate/5cd0392262781efcd65bb6672964c889.png)
class Solution {
public:
vector<vector<int>> result;
vector<int> path;
void backtracking(int k,int n,int startindex,int sum1){
if(path.size()==k && sum1==n){
result.push_back(path);
return;
}
for(int i=startindex;i<=9;i++){
path.push_back(i);
sum1+=i;
backtracking(k,n,i+1,sum1);
sum1-=path[path.size()-1];
path.pop_back();
}
}
vector<vector<int>> combinationSum3(int k, int n) {
backtracking(k,n,1,0);
return result;
}
};
17.电话号码的字母组合
class Solution {
public:
vector<string> result;
string path;
const string lettermap[10]={
"",
"",
"abc",
"def",
"ghi",
"jkl",
"mno",
"pqrs",
"tuv",
"wxyz"
};
void backtracking(string& digits,int index){
if(digits.size()==index){
result.push_back(path);
return;
}
int digit = digits[index]-'0';
string s = lettermap[digit];
for(int i=0;i<s.size();i++){
path+=s[i];
backtracking(digits,index+1);
path.pop_back();
}
}
vector<string> letterCombinations(string digits) {
if(digits.size()==0) return result;
backtracking(digits,0);
return result;
}
};