Letter Combinations of a Phone Number -- leetcode

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"].

Note:

Although the above answer is in lexicographical order, your answer could be in any order you want.

算法一: 用数制进位的思维做组合。

class Solution {
public:
    vector<string> letterCombinations(string digits) {
        const char *map[] = {" ", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
        int idx[sizeof(map)/sizeof(map[0])] = {0};
        vector<string> result;
        string item;
        bool carry = false;
        while (!carry) {
                item.clear();
                carry = true;
                for (int i=0; i<digits.size(); ++i) {
                        const char * p = &map[digits[i]-'0'][idx[i]];
                        if (*p)
                                item.append(1, *p);

                        if (carry && *p) {
                                ++idx[i];
                                ++p;
                                if (!*p)
                                        idx[i] = 0;
                                else
                                        carry = false;
                        }
                }
                result.push_back(item);
        }

        if (result.empty())
                result.push_back(item);

        return result;
    }
};

下面这一句,纯粹是为了满足leetcode的输入为空串("")时的test case:

        if (result.empty())
                result.push_back(item);

看到这算法的执行时间,忍不住做了个截图,留个纪念。

算法二,待续

时间: 2025-01-28 15:21:33

Letter Combinations of a Phone Number -- leetcode的相关文章

Letter Combinations of a Phone Number leetcode java

题目: 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"

Letter Combinations of a Phone Number——LeetCode

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", &q

Letter Combinations of a Phone Number [leetcode]谈谈循环解法的两种思路

本系列博文中有很多两种思路的,其实是因为第一遍刷题的时候有一个想法,第二遍刷题的时候已经忘掉之前的思路了,又有新的想法了. 同时大部分代码我也同时PO到leetcode的对应题目的问答中去了,所以如果你也查看问题讨论的话会发现有和我一模一样的代码,其实就是我PO的:) 书接正文,基于循环的两种思路如下: 第一种思路 比如"234"这个字符串,我可以先将0...1的所有排列找到-->{"a", "b", "c"} 再进一步

LeetCode: Letter Combinations of a Phone Number [018]

[题目] 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长征系列】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][JavaScript]Letter Combinations of a Phone Number

Letter Combinations of a Phone Number Total Accepted: 40709 Total Submissions: 157759My Submissions Question Solution Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just l

Leetcode:Letter Combinations of a Phone Number 手机键盘字母映射

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" Outp

【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", &q

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