微软算法题-按键映射
Code
ts
/**
* 根据数字按键,得到所有字母排列组合
* @param {string} digits 数字按键,例如'23'
* @param {string[]} 按键的所有排列组合
*/
function keyboardMap(digits) {}
解析
ts
function keyboardMap(digits) {
function _compose(str1, str2) {
let result = [];
for (let i = 0; i < str1.length; i++) {
for (let j = 0; j < str2.length; j++) {
result.push(str1[i] + str2[j]);
}
}
return result;
}
const map = [, , 'abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz'];
return digits
.split('')
.map((x) => map[x])
.reduce(_compose, []);
}