老规矩,大佬核心代码链接:https://blog.youkuaiyun.com/y18771025420/article/details/102776618
个人微改:
import java.util.*;
public class letterCombinations {
public static void main(String args[]) {
System.out.println("please enter your character string :");
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
List<String> hah = combinations(str);
System.out.println(hah);
sc.close();
}
static List<String> combinations(String digits){
List<String> list = new ArrayList<String>();
if(digits.length()==0) return list;
Map<Integer,String> map = new HashMap<Integer,String>();
map.put(2, "abc");
map.put(3, "def");
map.put(4, "ghi");
map.put(5, "jkl");
map.put(6, "mno");
map.put(7, "pqrs");
map.put(8, "tuv");
map.put(9, "wxyz");
for(int i=0;i<map.get(digits.charAt(0)-48).length();i++) {
list.add(map.get(digits.charAt(0)-48).substring(i,i+1));
}
for(int i=1;i<digits.length();i++) {
String str01 = map.get(digits.charAt(i)-48);
int len = list.size();
for(int k=0;k<len;k++) {
String s = list.get(0);
list.remove(0);
for(int m=0;m<str01.length();m++) {
list.add(s+str01.substring(m,m+1));
}
}
}
return list;
}
}
please enter your character string :
23
[ad, ae, af, bd, be, bf, cd, ce, cf]
该博客展示了如何用Java编程实现将电话号码(如23)转换为对应的字母组合(如ad,ae,af等)。通过创建一个映射表关联数字与字母,并递归地组合数字串来生成所有可能的字母组合。程序读取用户输入的电话号码字符串,然后输出所有可能的字母组合。
803

被折叠的 条评论
为什么被折叠?



