Toc
  1. Q:
    1. Example:
  • A:
  • Toc
    0 results found
    bbcfive
    Letter Combinations of a Phone Number
    2019/07/06 Algorithm Array

    Q:

    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.

    A:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    /**
    * @param {string} digits
    * @return {string[]}
    */
    var letterCombinations = function(digits) {
    var tmp = [" ", " ", "abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"];
    var ans = [];
    var digitsArr = digits.split('');
    var tmpArr = [];

    for(item in digitsArr) {
    tmpArr.push(tmp[digitsArr[item]])
    }

    if (tmpArr.length == 1) {
    ans = tmpArr[0].split('');
    return ans;
    } else if (tmpArr.length < 1) {
    return [];
    }

    ans = tmpArr[0];
    for (var i = 0; i < tmpArr.length - 1; i++) {
    ans = match(ans, tmpArr[i+1]);
    }

    function match(str1, str2) {
    var group = [];
    if(typeof str1 == 'string') {
    arr1 = str1.split('');
    } else {
    arr1 = str1;
    }

    arr2 = str2.split('');

    for (var m = 0; m < arr1.length; m++) {
    for (var n = 0; n < arr2.length; n++) {
    group.push(arr1[m] + arr2[n]);
    }
    }

    return group;
    }

    return ans;
    };
    本文作者:bbcfive
    版权声明:本文首发于bbcfive的博客,转载请注明出处!