zoukankan      html  css  js  c++  java
  • *的字母组合

    给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。

    给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。

    示例:

    输入:"23"
    输出:["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].

     1 public class T17 {
     2 
     3     public List<String> letterCombinations(String digits) {
     4         HashMap<Character, String> hashMap = new HashMap<>();
     5         hashMap.put('2', "abc");
     6         hashMap.put('3', "def");
     7         hashMap.put('4', "ghi");
     8         hashMap.put('5', "jkl");
     9         hashMap.put('6', "mno");
    10         hashMap.put('7', "pqrs");
    11         hashMap.put('8', "tuv");
    12         hashMap.put('9', "wxyz");
    13         List<String> list = new ArrayList<>();
    14         if (digits == null || digits.length() == 0) {
    15             return list;
    16         }
    17         getLeffters(digits, list,new StringBuilder(), 0,hashMap);
    18         return list;
    19 
    20     }
    21 
    22     private void getLeffters(String digits, List<String> list, StringBuilder stringBuilder, int i,HashMap<Character,String> hashMap) {
    23         if (i == digits.length()) {
    24             list.add(stringBuilder.toString());
    25             return;
    26         }
    27         String str = hashMap.get(digits.charAt(i));
    28 
    29         for (int j = 0; j < str.length(); j++) {
    30             stringBuilder.append(str.charAt(j));
    31             getLeffters(digits, list, stringBuilder, i + 1, hashMap);
    32             stringBuilder.deleteCharAt(stringBuilder.length() - 1);
    33         }
    34     }
    35 }
    一回生,二回熟
  • 相关阅读:
    Git 使用vi或vim
    git 添加远程仓库后无法push
    windows下使用IIS创建git服务
    NPOI 操作office、word、excel
    delphi 模拟POST提交数据
    git 用远程覆盖本地
    Delphi中MD5实现方法(转)
    Delphi 操作Ini文件
    Spring系列之——spring security
    Spring系列之——使用模板快速搭建springboot项目
  • 原文地址:https://www.cnblogs.com/zzytxl/p/12504936.html
Copyright © 2011-2022 走看看