Letter Combinations Of A Number Phone

 1 var arr = [
 2     [],
 3     [],
 4     ['a', 'b', 'c'],
 5     ['d', 'e', 'f'],
 6     ['g', 'h', 'i'],
 7     ['j', 'k', 'l'],
 8     ['m', 'n', 'o'],
 9     ['p', 'q', 'r', 's'],
10     ['t', 'u', 'v'],
11     ['w', 'x', 'y', 'z']
12 ];
13 
14 var letterCombinations = function(digits) {
15     if (digits.length < 1) {
16         return [];
17     }
18 
19     if (digits.length === 1) {
20         return arr[digits[0]];
21     }
22 
23     var ret = [],
24         listNow = arr[digits[0]],
25         listBehind = letterCombinations(digits.substring(1)),
26         lenBehind = listBehind.length;
27 
28     for (var i = 0; i < listNow.length; i++) {
29         for (var j = 0; j < listBehind.length; j++) {
30             ret[i * lenBehind + j] = listNow[i] + listBehind[j];
31         }
32     }
33 
34     return ret;
35 };

 

posted @ 2015-12-24 20:29  叫我霍啊啊啊  阅读(169)  评论(0编辑  收藏  举报