今日刷题两道:216.组合总和III, 17.电话号码的字母组合。
216.组合总和III
题目链接/文章讲解:https://programmercarl.com/0216.%E7%BB%84%E5%90%88%E6%80%BB%E5%92%8CIII.html
视频讲解:https://www.bilibili.com/video/BV1wg411873x
class Solution { private: vector<int> path; vector<vector<int>> result; void backtrating(int n, int k, int sum, int startindex){ if(path.size() == k){ if(n == sum){ result.push_back(path); return; } } for(int i=startindex;i<=9;i++){ sum+=i; path.push_back(i); backtrating(n,k,sum,i+1); sum-=i; path.pop_back(); } } public: vector<vector<int>> combinationSum3(int k, int n) { backtrating(n,k,0,1); return result; } };
17.电话号码的字母组合
视频讲解:https://www.bilibili.com/video/BV1yV4y1V7Ug
class Solution { private: const string letterMap[10] = { "", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz", }; public: vector<string> result; string s; void backtracking(const string& digits, int index){ if(index == digits.size()){ result.push_back(s); return; } int digit = digits[index] - '0'; string letters = letterMap[digit]; for(int i=0;i<letters.size();i++){ s.push_back(letters[i]); backtracking(digits,index+1); s.pop_back(); } }
vector<string> letterCombinations(string digits) { if(digits.size()==0) return result; backtracking(digits,0); return result;
} };
标签:digits,return,string,int,训练营,随想录,29,E7%,result From: https://www.cnblogs.com/zzw0612/p/17066706.html