Sunny-117 / js-challenges

✨✨✨ Challenge your JavaScript programming limits step by step

Home Page:https://juejin.cn/column/7244788137410560055

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

电话号码的字母组合

Pcjmy opened this issue · comments

commented
电话号码的字母组合
/**
 * @param {string} digits
 * @return {string[]}
 */
var letterCombinations = function(digits) {
    const len = digits.length
    const arr = ['','','abc','def','ghi','jkl','mno','pqrs','tuv','wxyz']
    if(!len) return []
    if(len === 1) return arr[digits].split('')

    const res = [], path = []
    backtracking(digits,len,0)
    return res

    function backtracking(n,len,arrIndex){
        if(path.length === len){
            res.push(path.join(""))
            return
        }
        for(const v of arr[n[arrIndex]]){
            path.push(v)
            backtracking(n,len,arrIndex + 1)
            path.pop()
        }
    }
};