LeetCode (17)Letter Combinations of a Phone Number

题目

Given a digit string, return all possible letter combinations that the number could represent.

A mapping of digit to letters (just like on the telephone buttons) is given below.

Input:Digit string “23”

Output: [“ad”, “ae”, “af”, “bd”, “be”, “bf”, “cd”, “ce”, “cf”].

分析

本题目要求求出多个字符串按字母全排得到的字符串集合。

我们知道求两个字符串的字母全排是简单的,只需要两次遍历,按照字母组合即可。

那么如何求多个字符串的全排呢?尤其是字符串的个数还是不固定的,对此(字符串大于等于3个时),我采用的解决办法是,先求出前两个的全排集合v,然后,逐次将集合中的字符串与第三个字符串的字母连接,得到新的集合。

详细思路见AC代码。

AC代码

class Solution {
public:
    vector<string> letterCombinations(string digits) {
        vector<string> vs;

        //求len为数字字符串的长度也对应字母字符串的个数
        int len = strlen(digits.c_str());
        if (len <= 0 )
            return vs;

        if (len == 1)
        {
            string str = letters(digits[0]);
            for (int i = 0; i < strlen(str.c_str()); i++)
            {
                string s = "";
                s += str[i];

                vs.push_back(s);
            }
            return vs;
        }

        //前两个单独处理

        string str1 = letters(digits[0]);
        string str2 = letters(digits[1]);
        for (int i = 0; i < strlen(str1.c_str()); i++)
        {
            for (int j = 0; j < strlen(str2.c_str()); j++)
            {
                string str = "";
                str = str + str1[i] + str2[j];
                vs.push_back(str);
            }
        }

        for (int i = 2; i < len; i++)
        {
            string str = letters(digits[i]);
            vs = Combine(vs, str);
        }
        return vs;
    }

    vector<string> Combine(const vector<string> &vs, const string &str2)
    {
        vector<string> v;

        int len = vs.size();
        if (len <= 0)
            return v;

        int len2 = strlen(str2.c_str());

        for (int i = 0; i < len; i++)
        {
            string str = vs[i];
            for (int j = 0; j < len2; j++)
            {
                v.push_back(str + str2[j]);
            }
        }//for

        return v;
    }

    string letters(const char &num)
    {
        string str = "";

        switch (num)
        {
        case ‘2‘:
            str = "abc"; break;
        case ‘3‘:
            str = "edf"; break;
        case ‘4‘:
            str = "ghi"; break;
        case ‘5‘:
            str = "jkl"; break;
        case ‘6‘:
            str = "mno"; break;
        case ‘7‘:
            str = "pqrs"; break;
        case ‘8‘:
            str = "tuv"; break;
        case ‘9‘:
            str = "wxyz"; break;
        default:
            str = "";
        }
        return str;
    }
};

GitHub测试程序源码

时间: 2024-10-06 00:33:43

LeetCode (17)Letter Combinations of a Phone Number的相关文章

LeetCode(17)Letter Combinations of a Phone Number

题目如下: Python代码: class Solution(object): def letterCombinations(self, digits): """ :type digits: str :rtype: List[str] """ if not digits: return [] dic = {'2':'abc','3':'def','4':'ghi','5':'jkl','6':'mno','7':'pqrs','8':'tuv',

【Leetcode】17、Letter Combinations of a Phone Number

题目 Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letter

【Leetcode长征系列】Letter Combinations of a Phone Number

原题: Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23" Output: ["ad", "ae"

leetcode第18题--Letter Combinations of a Phone Number

Problem: Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23" Output: ["ad", "ae&

LeetCode(17) - Letter Combinations of a Phone Number

经典的backtracking(回溯算法)的题目.当一个题目,存在各种满足条件的组合,并且需要把它们全部列出来时,就可以考虑backtracking了.当然,backtracking在一定程度上属于穷举,所以当数据特别大的时候,不合适.而对于那些题目,可能就需要通过动态规划来完成. 这道题的思路很简单,假设输入的是"23",2对应的是"abc",3对应的是"edf",那么我们在递归时,先确定2对应的其中一个字母(假设是a),然后进入下一层,穷举3

LeetCode(17):电话号码的字母组合

Medium! 题目描述: 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23" 输出:["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].

Leetcode之回溯法专题-17. 电话号码的字母组合(Letter Combinations of a Phone Number)

[Leetcode]17. 电话号码的字母组合(Letter Combinations of a Phone Number) 题目描述: 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23"输出:["ad", "ae", "af", "bd", "be", "bf&qu

leetCode 17. Letter Combinations of a Phone Number 字符串 | 回溯 | Medium

17. Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23"Ou

Leetcode 17. Letter Combinations of a Phone Number(水)

17. Letter Combinations of a Phone Number Medium Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given