电话号码的字母组合
Pcjmy opened this issue · comments
Pcjmy commented
电话号码的字母组合
Li Xiangyue 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()
}
}
};