zoukankan      html  css  js  c++  java
  • [LeetCode] Letter Combinations of a Phone Number

    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;
        }
    };
  • 相关阅读:
    Java SE 基础之接口回顾
    读书杂谈-《架构探险:从零开始写Java Web框架》
    Java Se之类加载问题思考
    struts2 下载记录
    《重构改善既有代码的设计》笔记之序
    Luence简单实现2
    RabbitMQ学习(1):安装
    jquery插件dataTables添加序号列
    父<IFRAME>获取子页属性以及子页中<IFRAME>的方法
    类的约束 异常处理 自定义异常 MD5 日志信息处理
  • 原文地址:https://www.cnblogs.com/jcliBlogger/p/4582179.html
Copyright © 2011-2022 走看看