[LeetCode] Letter Combinations of a Phone Number

简介: Well, a typical backtracking problem. Make sure you are clear with the following three problems: What is a partial solution and when is it finished?...

Well, a typical backtracking problem. Make sure you are clear with the following three problems:

  1. What is a partial solution and when is it finished? --- In this problem, the partial solution is a combination and it is finished once it is of the same length as digits.
  2. How to find all the partial solutions? --- In the following code, I use a nested for-loop to traverse all the possible starting elements for each digit.
  3. When to make recursive calls? --- In the following code, once an element is added to the partial solution, we call the function to generate the remaining elements recursively.

Of course, remember to recover to the previous status once a partial solution is done. In the following code, line 18 (comb.resize(comb.length() - 1)) is for this purpose.

The following should be self-explanatory :)

 1 class Solution {
 2 public:
 3     vector<string> letterCombinations(string digits) {
 4         string mp[] = {" ", " ", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
 5         vector<string> res;
 6         if (digits.empty()) return res;
 7         string comb;
 8         combinations(digits, 0, mp, comb, res);
 9         return res;
10     }
11 private:
12     void combinations(string& digits, int start, string mp[], string& comb, vector<string>& res) {
13         if (comb.length() == digits.length()) {
14             res.push_back(comb);
15             return;
16         }
17         for (int i = 0; i < (int)mp[digits[start] - '0'].length(); i++) {
18             comb += (mp[digits[start] - '0'][i]);
19             combinations(digits, start + 1, mp, comb, res);
20             comb.resize(comb.length() - 1);
21         }
22     }
23 };

 Well, the above recursive backtracking solution is a typical solution. In fact, this problem can also be solved iteratively. Refer to this link for more information. I have rewritten the code below for your reference.

class Solution {
public:
    vector<string> letterCombinations(string digits) {
        vector<string> res;
        if (digits.empty()) return res;
        string mp[] = {" ", " ", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
        res.push_back("");
        for (int i = 0; i < (int)digits.length(); i++) {
            string letters = mp[digits[i] - '0'];
            vector<string> temp;
            for (int j = 0; j < (int)letters.length(); j++)
                for (int k = 0; k < (int)res.size(); k++)
                    temp.push_back(res[k] + letters[j]);
            swap(res, temp);
        }
        return res;
    }
};

 

目录
相关文章
|
算法
Leetcode 313. Super Ugly Number
题目翻译成中文是『超级丑数』,啥叫丑数?丑数就是素因子只有2,3,5的数,7 14 21不是丑数,因为他们都有7这个素数。 这里的超级丑数只是对丑数的一个扩展,超级丑数的素因子不再仅限于2 3 5,而是由题目给定一个素数数组。与朴素丑数算法相比,只是将素因子变了而已,解法还是和朴素丑数一致的。
109 1
|
7月前
|
存储 SQL 算法
LeetCode 题目 65:有效数字(Valid Number)【python】
LeetCode 题目 65:有效数字(Valid Number)【python】
|
8月前
|
存储 算法
【LeetCode力扣】单调栈解决Next Greater Number(下一个更大值)问题
【LeetCode力扣】单调栈解决Next Greater Number(下一个更大值)问题
62 0
|
存储
Leetcode Single Number II (面试题推荐)
给你一个整数数组,每个元素出现了三次,但只有一个元素出现了一次,让你找出这个数,要求线性的时间复杂度,不使用额外空间。
43 0
LeetCode contest 190 5417. 定长子串中元音的最大数目 Maximum Number of Vowels in a Substring of Given Length
LeetCode contest 190 5417. 定长子串中元音的最大数目 Maximum Number of Vowels in a Substring of Given Length
LeetCode Contest 178-1365. 有多少小于当前数字的数字 How Many Numbers Are Smaller Than the Current Number
LeetCode Contest 178-1365. 有多少小于当前数字的数字 How Many Numbers Are Smaller Than the Current Number
LeetCode 136. 只出现一次的数字 Single Number
LeetCode 136. 只出现一次的数字 Single Number
LeetCode contest 177 5169. 日期之间隔几天 Number of Days Between Two Dates
LeetCode contest 177 5169. 日期之间隔几天 Number of Days Between Two Dates
|
人工智能 算法
LeetCode 1347. 制造字母异位词的最小步骤数 Minimum Number of Steps to Make Two Strings Anagram
LeetCode 1347. 制造字母异位词的最小步骤数 Minimum Number of Steps to Make Two Strings Anagram
LeetCode 1342. 将数字变成 0 的操作次数 Number of Steps to Reduce a Number to Zero
LeetCode 1342. 将数字变成 0 的操作次数 Number of Steps to Reduce a Number to Zero