首页 > 其他 > 详细

[?*]Letter Combinations of a Phone Number

时间:2015-11-17 08:21:59      阅读:292      评论:0      收藏:0      [点我收藏+]

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"].
 1   public class Solution {
 2         public static List<String> letterCombinations(String digits) {
 3             String digitletter[] = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
 4             List<String> result = new ArrayList<String>();
 5 
 6             if (digits.length()==0) return result;
 7 
 8             result.add("");
 9             for (int i=0; i<digits.length(); i++) 
10                 result = combine(digitletter[digits.charAt(i)-‘0‘],result);
11 
12             return result;
13         }
14 
15         public static List<String> combine(String digit, List<String> list) {
16             List<String> result = new ArrayList<String>();
17 
18             for (int i=0; i<digit.length(); i++) 
19                 for (String x : list) 
20                     result.add(x+digit.charAt(i));
21 
22             return result;
23         }
24     }

 

[?*]Letter Combinations of a Phone Number

原文:http://www.cnblogs.com/hygeia/p/4970619.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!