leetcode 17. 电话号码的字母组合

var letterCombinations = function(digits) {
    if (!digits || digits === ' ') return []
    let alpList = ['abc','def','ghi','jkl','mno','pqrs','tuv','wxyz']
    let res = []
    let len = digits.length - 1
    let tmpStr = []
    for(let i = 0;i <= len;i++){
        let num = digits[i]-2
        let alpCount = alpList[num].length - 1
        tmpStr = []
        let listLen = res.length - 1
        if(i === 0){
            for(let j = 0;j<=alpCount;j++){
                res.push(alpList[num][j])
            }
            continue
        }
        for(let k = 0;k<=listLen;k++){
            for(let j = 0;j<=alpCount;j++){
                tmpStr.push(res[k]+alpList[num][j])
            }
        }
        res = tmpStr
    }
    return res
};

思路:排列组合

posted @ 2020-04-16 18:12  想学JS的前端  阅读(152)  评论(0编辑  收藏  举报