#17. Letter Combinations of a Phone Number
这道题是一道状态更新的问题。基本的逻辑是,每增加一个新的数字,在之前得到的字符串数组中的每一个字符串之后增加这个数字所代表的字符。
var letterCombinations = function(digits) {
if(digits.length===0) {
return [];
}
let dict = {};
dict['2']='abc';
dict['3']='def';
dict['4']='ghi';
dict['5']='jkl';
dict['6']='mno';
dict['7']='pqrs';
dict['8']='tuv';
dict['9']='wxyz';
let res = [""];
for(let i=0;i<digits.length;i++) {
let newArr = [];
for(let j=0;j<res.length;j++) {
for(let m=0;m<dict[digits[i]].length;m++) {
newArr.push(res[j]+dict[digits[i]][m]);
}
}
res=newArr;
}
return res;
};