zoukankan      html  css  js  c++  java
  • 17. Letter Combinations of a Phone Number java solutions

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

    Note:
    Although the above answer is in lexicographical order, your answer could be in any order you want.

     1 public class Solution {
     2     public List<String> letterCombinations(String digits) {
     3         List<String> ans = new ArrayList<String>();
     4         if("".equals(digits) || digits == null) return ans;
     5         String[] numTochar = {"", "", "abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
     6         Combinations(ans,0,digits,"",numTochar);
     7         return ans;
     8     }
     9     
    10     public void Combinations(List<String> ans, int s,String digits, String tmp,String[] numTochar){
    11         if(s == digits.length()){
    12             ans.add(new String(tmp));
    13             return;
    14         }
    15         //for(int i = s; i < digits.length();i++){第一遍想错了,多了个循环 。一直AC不了
    16             int index = digits.charAt(s) - '0';
    17             for(int j = 0; j < numTochar[index].length(); j++){
    18                 Combinations(ans,s+1,digits,tmp + numTochar[index].charAt(j),numTochar);
    19             }
    20        // }
    21     }
    22 }
  • 相关阅读:
    tp 30秒超时
    zend studio git 提示错误
    php连接数据库
    php点击排序
    表情符号解析
    js Object.assign 方法
    Web界面简繁体转换
    H5骨架屏实战
    JavaScript heap out of memory问题
    Vue bus插件封装
  • 原文地址:https://www.cnblogs.com/guoguolan/p/5625831.html
Copyright © 2011-2022 走看看