Sat, 30 Nov 2019 10:59:18 +0000
  • A (Algotithm) 至少做一个leetcode的算法题
  • R (Review) 阅读并点评一篇英文的技术文章
  • T (Tip) 学习一个技术技巧
  • S (Share) 分享一篇有观点和思考的技术文章

每周一次,坚持一年

Algorithm

Description Letter Combinations of a Phone Number

Solution

/**
 * @param {string} digits
 * @return {string[]}
 */
var letterCombinations = function(digits) {
    digits = String(digits);
    if (digits.length === 1) {
        return digitMaps[digits].split('');
    }
    if (digits === '') {
        return [];
    }
    var firstDigit = digits[0];
    var lastDigits = digits.slice(1);
    var firstStr = digitMaps[firstDigit];
    var result = [];
    firstStr.split('').forEach(firstItem => {
        letterCombinations(lastDigits).forEach(lastItem => {
            result.push(firstItem + lastItem);
        });
    });
    return result;
};

const digitMaps = {
    2: 'abc',
    3: 'def',
    4: 'ghi',
    5: 'jkl',
    6: 'mno',
    7: 'pqrs',
    8: 'tuv',
    9: 'wxyz',
};


// letterCombinations(233);

Review

Tip

Share



blog comments powered by Disqus