• Letter Combinations of a Phone Number


    题目:

    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"].

    思路:

    没啥思路,上来生做。。。就是permutation。一个bug就是,在check input之后,加一个空string到res里。

     1     public List<String> letterCombinations(String digits) {
     2         List<String> res = new ArrayList<String>();
     3         if (digits == null || digits.length() == 0) {
     4             return res;
     5         }
     6         //BUG: add empty string first! after dealing with the coner case.
     7         res.add("");
     8 
     9         int len = digits.length();
    10         HashMap<Character, String> key = new HashMap<Character, String>();
    11         key.put('1',"");
    12         key.put('2',"abc");
    13         key.put('3',"def");
    14         key.put('4',"ghi");
    15         key.put('5',"jkl");
    16         key.put('6',"mno");
    17         key.put('7',"pqrs");
    18         key.put('8',"tuv");
    19         key.put('9',"wxyz");
    20         
    21         for (int i = 0; i < len; i++) {
    22             char digit = digits.charAt(i);
    23             String letter = key.get(digit);
    24             ArrayList<String> newRes = new ArrayList<String>();
    25             for (int j = 0; j < letter.length(); j++) {
    26                 for (String t : res) {
    27                     String subRes = t + letter.charAt(j);
    28                     newRes.add(subRes);
    29                 }
    30             }
    31             res = newRes;
    32         }
    33         return res;
    34     }
  • 相关阅读:
    LVS 模式
    修改RocketMQ的NameServer端口
    一次清理Hbase的oldWALs的过程
    Linux下删除文件系统空间不释放的问题
    HBase 强制删除表
    关闭Found duplicated code
    Java操作HDFS代码样例
    RocketMQ:Cannot allocate memory
    Storm的StreamID使用样例(版本1.0.2)
    android studio 编译sdk版降低报错解决方法
  • 原文地址:https://www.cnblogs.com/gonuts/p/4475159.html
Copyright © 2020-2023  润新知