方法:回溯—-for循环+递归,不用考虑递归结束条件,for循环结束递归结束且在递归结束时输出结果
    string的用法要注意

    1. class Solution {
    2. public:
    3. unordered_map<char, string> phoneMap{
    4. {'2', "abc"},
    5. {'3', "def"},
    6. {'4', "ghi"},
    7. {'5', "jkl"},
    8. {'6', "mno"},
    9. {'7', "pqrs"},
    10. {'8', "tuv"},
    11. {'9', "wxyz"}
    12. };
    13. vector<string> results;
    14. vector<string> letterCombinations(string digits) {
    15. if(digits.empty()){
    16. return results;
    17. }
    18. string result;
    19. combination(digits,0,result,results);
    20. return results;
    21. }
    22. void combination(string& digits,int index,string& result,vector<string>&results){
    23. if(index==digits.size()){
    24. results.emplace_back(result);
    25. }
    26. char temp=digits[index];
    27. string pos=phoneMap[temp];
    28. for(auto p:pos){
    29. result+=p;
    30. combination(digits,index+1,result,results);
    31. result.pop_back();
    32. }
    33. }
    34. };