手机的上中文九宫格按键排布
输入:
一串从2到9的数字组成的字符串
输出:
所有可能的字母组合
思路:
单纯地以字节为单位,遍历输入的字符并循环添加可能的字符肯定是行不通的,因为这样复杂度会以n次幂指数增长.
解决方法就是保存结果字符串数组
res
,然后以res
数组的长度为循环,往每一项后面加上新的字符,将其存入新的临时数组tmp
中,再将tmp
保存为新的res
数组,最后res
数组即为答案了.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 var map = {2:["a","b","c"],
3:["d","e","f"],
4:["g","h","i"],
5:["j","k","l"],
6:["m","n","o"],
7:["p","q","r","s"],
8:["t","u","v"],
9:["w","x","y","z"],
}
function make(str){
var res = [""];
for(let i =0; i<str.length; i++){
var tmp = [];
for(let j =0; j<res.length; j++){
let num = str[i];
for(let k=0; k<map[num].length; k++){
tmp.push(res[j]+map[num][k])
}
}
res = tmp
}
return res
}
//var str =readline();
//print(make(str))
str="996"
console.log(make(str))