码迷,mamicode.com
首页 > 其他好文 > 详细

Letter Combinations Of A Number Phone

时间:2015-12-24 20:42:47      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:

 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 };

 

Letter Combinations Of A Number Phone

标签:

原文地址:http://www.cnblogs.com/HuoAA/p/5074227.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!