|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- Given a string containing digits from 2-9 inclusive, 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. Note that 1 does not map to any letters.
- Example:
- Input: "23"
- Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
- Note:
- Although the above answer is in lexicographical order, your answer could be in any order you want.
复制代码
- class Solution {
- public static List<String> letterCombinations(String digits) {
-
- List<String> fi = new ArrayList<String>();
-
- if(digits == null || digits.length() == 0){
-
- return fi;
- }
-
- generateString(digits, fi , "");
-
- return fi;
-
- }
-
- public static void generateString(String digits, List<String> fi,String result){
-
- if(digits.length() == 0) {
-
- fi.add(result);
-
- return;
- }
-
- String head = get_head(digits);
-
- String a = "";
-
-
- switch (head) {
- case "2":
- a = "abc";
- break;
- case "3":
- a = "def";
- break;
- case "4":
- a = "ghi";
- break;
- case "5":
- a = "jkl";
- break;
- case "6":
- a = "mno";
- break;
- case "7":
- a = "pqrs";
- break;
- case "8":
- a = "tuv";
- break;
- case "9":
- a = "wxyz";
- break;
- default:
- a = "";
- break;
- }
-
- for(int i = 0; i < a.length(); i++) {
-
- result = result + a.substring(i,i+1);
-
- generateString(digits.substring(1,digits.length()),fi,result);
-
- result = result.substring(0,result.length()-1);
-
- }
-
- }
-
- public static String get_head(String digits){
-
- if(digits == null || digits == ""){
-
- return null;
- }
-
- return digits.substring(0,1);
- }
- }
复制代码
|
|