图片授权基于 www.pixabay.com 相关协议
const letterCombinations = function (digits) {
const len = digits.length
if (digits === null || !len) return []
const phone = {
2: \\\'abc\\\',
3: \\\'def\\\',
4: \\\'ghi\\\',
5: \\\'jkl\\\',
6: \\\'mno\\\',
7: \\\'pqrs\\\',
8: \\\'tuv\\\',
9: \\\'wxyz\\\',
}
//存储最终结果
const res = [];
const mergeFn = (i, s) => {
if (i === len) {
res.push(s)
return
}
for (const dig of phone[digits[i]]) {
mergeFn(i + 1, s + dig)
}
}
mergeFn(0, \\\'\\\')
return res
}
参考资料:
https://leetcode-cn.com/problems/letter-combinations-of-a-phone-number/solution/dian-hua-hao-ma-de-zi-mu-zu-he-by-leetcode-solutio/
https://leetcode.com/problems/letter-combinations-of-a-phone-number/discuss/139447/Clean-JavaScript-DFS-solution
原创文章,作者:小道研究,如若转载,请注明出处:https://www.sudun.com/ask/34497.html