代码随想录算法训练营第二十五天 | 216. 组合总和 III、17. 电话号码的字母组合
- 216. 组合总和 III
- 题目
- 解法
- 17. 电话号码的字母组合
- 题目
- 解法
- 感悟
216. 组合总和 III
题目
解法
- 修改上一天组合的代码
class Solution {
public:vector<vector<int>> result;vector<int> path;void backtracking(int n, int k, int sum_n, int startIdx) {// 终止条件if (path.size() == k) {int sum = 0;for (int i = 0; i < path.size(); i++) {sum += path[i];}if(sum == sum_n) result.push_back(path);return;}for (int i = startIdx; i <= n-(k-path.size())+1; i++) { // 剪枝优化path.push_back(i); // 处理的节点backtracking(n, k, sum_n, i+1);path.pop_back(); // 回溯,撤销处理的节点}return ;}vector<vector<int>> combinationSum3(int k, int n) {backtracking(9, k, n, 1);return result;}
};
2.看过题解之后的简化:减低时间复杂度、优化剪枝
class Solution {
public:vector<vector<int>> result;vector<int> path;void backtracking(int sum, int k, int sum_n, int startIdx) {if (sum > sum_n) return; //剪枝// 终止条件if (path.size() == k) {// int sum = 0;// for (int i = 0; i < path.size(); i++) { // 减去时间复杂度// sum += path[i];// }if(sum == sum_n) result.push_back(path);return;}for (int i = startIdx; i <= 9-(k-path.size())+1; i++) { // 剪枝优化sum += i;path.push_back(i); // 处理的节点backtracking(sum, k, sum_n, i+1);path.pop_back(); // 回溯,撤销处理的节点sum -= i;}return ;}vector<vector<int>> combinationSum3(int k, int n) {result.clear();path.clear();backtracking(0, k, n, 1);return result;}
};
17. 电话号码的字母组合
题目
解法
- 自己解法:按照昨天代码进行改进,代码不够简洁
class Solution {
public:vector<string> result;string path;void backtracking(string digits, int startIdx) {if(path.size() == digits.size()) {result.push_back(path);return ;}for(int i = startIdx; i < digits.size(); i++) {if(digits[i] == '2'){path += 'a';backtracking(digits, i+1);path.pop_back();path += "b";backtracking(digits, i+1);path.pop_back();path += "c";backtracking(digits, i+1);path.pop_back();}else if(digits[i] == '3'){path += "d";backtracking(digits, i+1);path.pop_back();path += "e";backtracking(digits, i+1);path.pop_back();path += "f";backtracking(digits, i+1);path.pop_back();}else if(digits[i] == '4'){path += "g";backtracking(digits, i+1);path.pop_back();path += "h";backtracking(digits, i+1);path.pop_back();path += "i";backtracking(digits, i+1);path.pop_back();}else if(digits[i] == '5'){path += "j";backtracking(digits, i+1);path.pop_back();path += "k";backtracking(digits, i+1);path.pop_back();path += "l";backtracking(digits, i+1);path.pop_back();}else if(digits[i] == '6'){path += "m";backtracking(digits, i+1);path.pop_back();path += "n";backtracking(digits, i+1);path.pop_back();path += "o";backtracking(digits, i+1);path.pop_back();}else if(digits[i] == '7'){path += "p";backtracking(digits, i+1);path.pop_back();path += "q";backtracking(digits, i+1);path.pop_back();path += "r";backtracking(digits, i+1);path.pop_back();path += "s";backtracking(digits, i+1);path.pop_back();}else if(digits[i] == '8'){path += "t";backtracking(digits, i+1);path.pop_back();path += "u";backtracking(digits, i+1);path.pop_back();path += "v";backtracking(digits, i+1);path.pop_back();}else if(digits[i] == '9'){path += "w";backtracking(digits, i+1);path.pop_back();path += "x";backtracking(digits, i+1);path.pop_back();path += "y";backtracking(digits, i+1);path.pop_back();path += "z";backtracking(digits, i+1);path.pop_back();}}return ;}vector<string> letterCombinations(string digits) {result.clear();path.clear();if(digits.empty()) return result;backtracking(digits, 0);return result;}
};
2.看完题解之后,代码优化
class Solution {
private:const string letterMap[10] = { //减少代码量"",//0"",//1"abc",//2"def",//3"ghi",//4"jkl",//5"mno",//6"pqrs",//7"tuv",//8"wxyz",//9};
public:vector<string> result;string path;void backtracking(const string& digits, int index) {if(index == digits.size()) {result.push_back(path);return ;}int id = digits[index] - '0';string letter = letterMap[id];for (int i = 0; i < letter.size(); i++) {path.push_back(letter[i]);backtracking(digits, index+1);path.pop_back();}return ;}vector<string> letterCombinations(string digits) {result.clear();path.clear();if(digits.empty()) return result;backtracking(digits, 0);return result;}
};
感悟
重复的代码可以通过规律固定下来,达到简化代码的效果