这道题考察的是回溯算法,而且是典型的回溯组合,没人用js提交,我来分享下我的代码
function phoneNumber( num ) {
const map = {
"2": "ABC",
"3": "DEF",
"4": "GHI",
"5": "JKL",
"6": "MNO",
"7": "PQRS",
"8": "TUV",
"9": "WXYZ",
}
const res = []
const temp = []
let index = 0
function way(index) {
if(num.length === temp.length) {
res.push(temp.join(""))
return
}
let letter = map[num[index]]
for(let i = 0; i < letter.length; ++i) {
temp.push(letter[i])
way(index+1)
temp.pop()
}
}
way(index)
return res
}
module.exports = {
phoneNumber : phoneNumber
};