ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
![](https://box.kancloud.cn/edeb597f83c7fafbc05782105b391e70_617x515.png) ### 解题思路 ![](https://img.kancloud.cn/00/a8/00a85afa6161085eba2a9fc0723a61d3_996x511.png) * 设置对象映射 * 遍历对应的字符串 * 第一次将字符串放到结果中 * 第二次及以后就双遍历生成新的字符串数组 ``` /** * @param {string} digits * @return {string[]} */ var letterCombinations = function(digits) { let result = [] if(digits.length == 0) return [] let numMap = { 2: 'abc', 3: 'def', 4: 'ghi', 5: 'jkl', 6: 'mno', 7: 'pqrs', 8: 'tuv', 9: 'wxyz' } for(let code of digits){ let word = numMap[code] if(result.length > 0){ let tmp = [] for(let char of word){ for(let old of result){ tmp.push(old+char) } } result = tmp }else{ result.push(...word) } } return result }; ``` ``` var letterCombinations = function(digits) { let len = digits.length; if(!len) return []; let dictionary = [[],[],['a','b','c'],['d','e','f'], ['g','h','i'],['j','k','l'],['m','n','o'], ['p','q','r','s'],['t','u','v'],['w','x','y','z']]; let res = [...dictionary[Number(digits[0])]]; console.log('res',res); for(let i = 1; i < len; i ++) { let temp = []; let arr = [...dictionary[Number(digits[i])]]; console.log('arr',arr) let resLen = res.length, arrLen = arr.length; for(let x = 0; x < resLen; x ++) { for(let y = 0; y < arrLen; y ++) { console.log('res[x]',res[x]) console.log('res[y]',res[y]) temp.push(res[x] + arr[y]); } } res = temp; } return res; }; ```