- 17. Letter Combinations of a Pho
- 17. Letter Combinations of a Pho
- 17. Letter Combinations of a Pho
- 17. Letter Combinations of a Pho
- 17. Letter Combinations of a Pho
- 17. Letter Combinations of a Pho
- 17. Letter Combinations of a Pho
- 17. Letter Combinations of a Pho
- 17. Letter Combinations of a Pho
- 17. Letter Combinations of a Pho
Given a digit string, return all possible letter combinations that the number could represent.
A mapping of digit to letters (just like on the telephone buttons) is given below.
Input:Digit string "23"Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
手机九宫格按键,给出数字序列,输出所有可能的字母序列
就很直接的实现就好,根据上一个按键的结果,生成所有可能的本次按键的结果
var letterCombinations = function(digits) {
var num = digits.length;
if (num===0)
return [];
if (digits[0]==='0')
return [];
var map = [[],['*'],['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']];
var result = map[parseInt(digits[0])].concat();
for (let i = 1;i < num;i++) {
if (digits[i]==='0')
return [];
var mapNow = map[parseInt(digits[i])];
var lastLength = result.length;
for (let j = 0;j < lastLength;j++) {
var base = result.shift();
for (let k = 0;k < map[parseInt(digits[i])].length;k++) {
result.push(base+map[parseInt(digits[i])][k]);
}
}
}
return result;
};
网友评论