鱼C论坛

 找回密码
 立即注册
查看: 1412|回复: 0

[学习笔记] leetcode 17. Letter Combinations of a Phone Number 谷歌面试题

[复制链接]
发表于 2019-8-18 04:39:53 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent.

  2. A mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.


  3. Example:

  4. Input: "23"
  5. Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
  6. Note:

  7. Although the above answer is in lexicographical order, your answer could be in any order you want.
复制代码


200px-Telephone-keypad2.svg.png

  1. class Solution {
  2.     public static List<String> letterCombinations(String digits) {
  3.         
  4.         List<String> fi  = new ArrayList<String>();
  5.         
  6.         if(digits == null || digits.length() == 0){
  7.             
  8.             return fi;
  9.         }
  10.         
  11.         generateString(digits, fi , "");
  12.         
  13.         return fi;
  14.         
  15.     }
  16.    
  17.     public static void generateString(String digits, List<String> fi,String result){
  18.            
  19.             if(digits.length() == 0) {
  20.                    
  21.                     fi.add(result);
  22.                    
  23.                     return;
  24.             }
  25.         
  26.             String head = get_head(digits);
  27.            
  28.             String a = "";
  29.            
  30.            
  31.             switch (head) {
  32.         case "2":
  33.             a = "abc";
  34.             break;
  35.         case "3":
  36.             a = "def";
  37.             break;
  38.         case "4":
  39.             a = "ghi";
  40.             break;
  41.         case "5":
  42.             a = "jkl";
  43.             break;
  44.         case "6":
  45.             a = "mno";
  46.             break;
  47.         case "7":
  48.             a = "pqrs";
  49.             break;
  50.         case "8":
  51.             a = "tuv";
  52.             break;
  53.         case "9":
  54.             a = "wxyz";
  55.             break;
  56.         default:
  57.             a = "";
  58.             break;
  59.         }
  60.            
  61.             for(int i = 0; i < a.length(); i++) {
  62.                    
  63.                     result = result + a.substring(i,i+1);
  64.                    
  65.                     generateString(digits.substring(1,digits.length()),fi,result);
  66.                    
  67.                     result = result.substring(0,result.length()-1);
  68.                    
  69.             }
  70.         
  71.     }
  72.    
  73.     public static String get_head(String digits){
  74.         
  75.         if(digits == null || digits == ""){
  76.             
  77.             return null;
  78.         }
  79.         
  80.         return digits.substring(0,1);
  81.     }
  82. }
复制代码


18.jpg

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-20 20:56

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表