public class Solution {
static String[] map = new String[10];
public List<String> letterCombinations(String digits) {
map[0] = "";
map[1] = "";
map[2] = "abc";
map[3] = "def";
map[4] = "ghi";
map[5] = "jkl";
map[6] = "mno";
map[7] = "pqrs";
map[8] = "tuv";
map[9] = "wxyz";
return help(digits, 0);
}
public List<String> help(String digits,int start)
{
List<String> result=new LinkedList<String>();
if(start<digits.length())
{
String array=map[digits.charAt(start)-'0'];
List<String> temp=help(digits, start+1);
if(temp.isEmpty())
{
for(int i=0;i<array.length();i++)
{
result.add(""+array.charAt(i));
}
}
else
{
for(int i=0;i<array.length();i++)
{
for (String string : temp) {
result.add(""+array.charAt(i)+string);
}
}
}
}
return result;
}
}
leetcode Letter Combinations of a Phone Number
数字组合生成算法解析
最新推荐文章于 2024-01-18 13:57:19 发布
本文详细阐述了如何通过编程实现数字到字母组合的转换,利用预定义的映射表实现字符串构建过程,通过递归调用高效生成所有可能的组合。
169

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



