【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 letters.

Example:

Input: "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.

思路

关键就是两个循环,在原有X的基础上,比如新加数字3,则Xd Xe Xf

class Solution {
public:
    vector<string> letterCombinations(string digits) {
        vector<string> result;
        if(digits.empty())
            return vector<string>();
        vector<string> v = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
        result.push_back("");   // add a seed for the initial case

        for(int i = 0 ; i < digits.size(); ++i) {
            int num = digits[i]-‘0‘;
            if(num < 0 || num > 9)
                break;
            string candidate = v[num];
            if(candidate.empty())
                continue;
            vector<string> tmp;
            for(int j = 0 ; j < candidate.size() ; ++j) {
                for(int k = 0 ; k < result.size() ; ++k) {
                    tmp.push_back(result[k] + candidate[j]);
                }
            }
            result = tmp;
        }
        return result;
    }
};

Explanation with sample input "123"

Initial state:

  • result = {""}

Stage 1 for number "1":

  • result has {""}
  • candiate is "abc"
  • generate three strings "" + "a", ""+"b", ""+"c" and put into tmp,
    tmp = {"a", "b","c"}
  • swap result and tmp (swap does not take memory copy)
  • Now result has {"a", "b", "c"}

Stage 2 for number "2":

  • result has {"a", "b", "c"}
  • candidate is "def"
  • generate nine strings and put into tmp,
    "a" + "d", "a"+"e", "a"+"f",
    "b" + "d", "b"+"e", "b"+"f",
    "c" + "d", "c"+"e", "c"+"f"
  • so tmp has {"ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf" }
  • swap result and tmp
  • Now result has {"ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf" }

Stage 3 for number "3":

  • result has {"ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf" }
  • candidate is "ghi"
  • generate 27 strings and put into tmp,
  • add "g" for each of "ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"
  • add "h" for each of "ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"
  • add "h" for each of "ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"
  • so, tmp has
    {"adg", "aeg", "afg", "bdg", "beg", "bfg", "cdg", "ceg", "cfg"
    "adh", "aeh", "afh", "bdh", "beh", "bfh", "cdh", "ceh", "cfh"
    "adi", "aei", "afi", "bdi", "bei", "bfi", "cdi", "cei", "cfi" }
  • swap result and tmp
  • Now result has
    {"adg", "aeg", "afg", "bdg", "beg", "bfg", "cdg", "ceg", "cfg"
    "adh", "aeh", "afh", "bdh", "beh", "bfh", "cdh", "ceh", "cfh"
    "adi", "aei", "afi", "bdi", "bei", "bfi", "cdi", "cei", "cfi" }

Finally, return result.

原文地址:https://www.cnblogs.com/shiganquan/p/9395367.html

时间: 2024-10-13 05:08:10

【Leetcode】17、Letter Combinations of a Phone Number的相关文章

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

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',

【ThinkingInC++】17、使用函数指针

/** * 功能:使用函数指针 * 时间:2014年8月14日07:23:42 * 作者:cutter_point */ #include<iostream> #include<cstdlib> using namespace std; void fun1() { cout<<"The function fun1 called.."<<endl; } int main() { void (*fp)(); //一个指向不需要参数的函数返回v

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

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&

【剑指offer】19、正则表达式匹配 &amp;&amp; 【Leetcode】44、Wildcard Matching

题目一 请实现一个函数用来匹配包括'.'和'*'的正则表达式.模式中的字符'.'表示任意一个字符,而'*'表示它前面的字符可以出现任意次(包含0次). 在本题中,匹配是指字符串的所有字符匹配整个模式.例如,字符串"aaa"与模式"a.a"和"ab*ac*a"匹配,但是与"aa.a"和"ab*a"均不匹配 思路 用动态规划来解决 规定 若,则str[0~i-1]和pattern[0~j-1]匹配 1.当p[j

LeetCode(17) - Letter Combinations of a Phone Number

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

【LEETCODE】39、第561题 Array Partition I

package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * @ClassName: ArrayPairSum * @Author: xiaof * @Description: 561. Array Partition I * Given an array of 2n integers, your task is to group these integers

【LEETCODE】42、922. Sort Array By Parity II

package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * @ClassName: SortArrayByParityII * @Author: xiaof * @Description: 922. Sort Array By Parity II * Given an array A of non-negative integers, half of the