题目及测试
package pid017;
/* 电话号码的字母组合
给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。
给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。
2 abc
3 def
4 ghi
5 jkl
6 mno
7 pqrs
8 tuv
9 wxyz
示例:
输入:"23"
输出:["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
说明:
尽管上面的答案是按字典序排列的,但是你可以任意选择答案输出的顺序。
*/
import java.util.List;
public class main {
public static void main(String[] args) {
String[] testTable = {"24","23","6574"};
for (String ito : testTable) {
test(ito);
}
}
private static void test(String ito) {
Solution solution = new Solution();
List<String> rtn;
long begin = System.currentTimeMillis();
System.out.println("ito="+ito);
System.out.println();
//开始时打印数组
rtn= solution.letterCombinations(ito);//执行程序
long end = System.currentTimeMillis();
System.out.println("rtn=" );
for(int i=0;i<rtn.size();i++){
System.out.print(rtn.get(i)+" ");
}
System.out.println();
System.out.println("耗时:" + (end - begin) + "ms");
System.out.println("-------------------");
}
}
解法1(成功,5ms,较慢)
在类中建立一个数字对应字母的map,和对应的结果list
进入combine("",digits)
第一个字符串为当前字符串,后面的为还能加上的数字字符串
每次从数组字符串中取最前一位,得到对应的char数组,将nowStr+对应的数组(用for循环,分别加几次),然后再次combine
如果remain为“”,说明一小次循环完成,将nowstr加入result
package pid017;
import java.sql.Array;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
public class Solution {
/*2 abc
3 def
4 ghi
5 jkl
6 mno
7 pqrs
8 tuv
9 wxyz*/
HashMap<Character, char[]> map=new HashMap<>();
List<String> result=new ArrayList<>();
public List<String> letterCombinations(String digits) {
int length=digits.length();
if(length==0){
return result;
}
map.put('2', new char[]{'a','b','c'});
map.put('3', new char[]{'d','e','f'});
map.put('4', new char[]{'g','h','i'});
map.put('5', new char[]{'j','k','l'});
map.put('6', new char[]{'m','n','o'});
map.put('7', new char[]{'p','q','r','s'});
map.put('8', new char[]{'t','u','v'});
map.put('9', new char[]{'w','x','y','z'});
combine("",digits);
return result;
}
public void combine(String nowStr,String remain){
int length=remain.length();
if(length==0){
result.add(nowStr);
return;
}
Character now=remain.charAt(0);
for(char nowChar:map.get(now)){
combine(nowStr+nowChar,remain.substring(1));
}
}
}
解法2(成功,1ms,极快思路一样)
package pid017;
import java.sql.Array;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
public class Solution {
/*2 abc
3 def
4 ghi
5 jkl
6 mno
7 pqrs
8 tuv
9 wxyz*/
public List<String> letterCombinations(String digits) {
List<String> result=new ArrayList<>();
if(digits.length()==0){
return result;
}
combine(digits, result, "", 0);
return result;
}
public void combine(String digits,List<String> result,String beforString,int nowIndex){
if(nowIndex>=digits.length()){
result.add(beforString);
return;
}
char[] nowChars=getCharsByNum(digits.charAt(nowIndex));
for(int i=0;i<nowChars.length;i++){
combine(digits, result, beforString+nowChars[i], nowIndex+1);
}
}
public Map<Character, char[]> map=new HashMap<>();
{
map.put('2', new char[]{'a','b','c'});
map.put('3', new char[]{'d','e','f'});
map.put('4', new char[]{'g','h','i'});
map.put('5', new char[]{'j','k','l'});
map.put('6', new char[]{'m','n','o'});
map.put('7', new char[]{'p','q','r','s'});
map.put('8', new char[]{'t','u','v'});
map.put('9', new char[]{'w','x','y','z'});
}
public char[] getCharsByNum(char num){
return map.get(num);
}
}