我需要一些关于我为一个项目编写的方法的帮助。该方法将电话号码更改为文本字符串列表。
你知道2-9在电话上有与它们相关的字母。我想做一个转换器,将改变一个7位数字的字符串列表。我想看看所有的可能性。我已经去掉了所有的1和0,因为它们没有任何字母。例如:如果我们的号码只有两位数长,37就是:
DP DQ DR DS EP EQ ER ES FP FQ FR FS。
到目前为止,我一直在尝试使用嵌套的for循环,但是没有得到正确的输出。任何帮助或想法都会很好。谢谢
(我并不是在要求完整的代码,而是更像是关于如何做的建议)
发布于 2009-12-07 16:42:56
解决方案的关键是使用下面代码中声明的pad数组。例如,在部分电话号码763中,
pad7将产生数组{'p','q','r'},
pad6将生成数组{'m','n','o'},
pad3将生成数组{'a','b','c'},
然后,使用递归方法getAlpha(int[] num,int next,char[]alpha)迭代每个组合可能性,形成按字母顺序排列的算法树。在树的每个叶/末端节点,是要附加到列表中的字母的完整数组。当其递归回到先前的数字位置时,可以仅使用一个字母表阵列来重用/重写,因为仅当到达叶/结束节点时才附加字符串化的字母表阵列。此处不包含stringify(char[])。
getAlpha(int[] num)是从递归的数字位置0开始的入口点方法。每个递归级别处理电话号码的下一个数字。
public class Z{
// 2D array [i][j]
// use phone digit as array index i
final char[][] pad = {
{'0'},
{'1'},
{'a','b','c'},
{'d','e','f'},
{'g','h','i'},
{'j','k','l'},
{'m','n','o'},
{'p','q','r'},
{'s','t','u','v'},
{'w','x','y','z'},
};
// This will be the horrendously long list of possible alphabetic codes
List<String> combinations = new ArrayList<String>();
void getAlpha(int[] num, int next, char[]alpha){
// iterate over all possible alphabets of next digit
for (int i=0; i<pad[next].length; i++){
//set,overwrite next cell of array with iterated alphabet
alpha[next] = pad[next][i];
if (i<num.length-1)
//process next next digit
getAlpha(num, next++, alpha);
else
//if end of number
//append array to horrendously long list
combinations.add(stringify(alpha));
}
}
public void getAlpha(int[] num){
getAlpha(num, 0, new char[num.length]);
}
}发布于 2009-12-07 15:14:08
如果我没记错的话,任何数字都不超过4个字母。
一种粗略的生成方法是在基数4中从0到4^(位数)-1,这将得到类似于02031的数字,让0代表相关数字的第一个字母,1代表第二个数字,依此类推。在数字只有3个字母的位置上包含3的所有数字都将被丢弃。
一个10位数字将产生一个超过一百万个以4为基数的数字列表。你已经被警告过了。
编辑:一种更优雅的方法是查看您有多少个4(让我们称之为x)字符数字和3(让我们称之为y)个字符数字,并从0到4^x*3^y-1。通过使用/和%运算符,可以将每个数字转换为如上所示的一系列数字。
示例:如果8和9是4个字符的数字,并且您想要数字258的字符串表示列表,则从0数到3^2*4-1 = 35。以数字21为例:向后计算,最右边的字符(从8开始),通过
21 %4= 1,1代表't‘
除以这个字符的信息,你就会得到21 /4= 5。
下一个字符:
5%3= 2,2代表'l‘
5/3= 1。
最后一个字符:
1%3=1表示'b‘
这会得到字符串"blt“。
此算法有一些记账功能,但您不会从上面的示例中获得开销计数和丢弃,也不会获得递归算法所具有的内存开销。
发布于 2009-12-07 15:32:09
您可能想要使用递归。你说过不要给你代码,所以这里只有一个提纲:
// This is the function you're writing. It prints every possible
// string you can make with the digits of that phone number by
// calling another function recursively.
void printAllPossibilities(String phoneNumber) {
recursivePrintAllPossibilities("", phoneNumber);
}
void recursivePrintAllPossibilities(String prefix, String phoneNumber) {
// 1. If the phone number is empty, just print the prefix and return.
// 2. If the phone number is not empty, take the first digit and convert it
// into possible letters. For each letter, add that letter to the prefix
// and then call recursivePrintAllPossibilities with the new prefix, and
// with the now-truncated phone number.
}给定您的示例输入"37",主函数printAllPossibilities将使用prefix="“和phoneNumber="37”调用recursivePrintAllPossibilities。该函数将调用自身三次:
一起使用
第一个调用将再次调用自身四次:
带有phoneNumber=""
的phoneNumber=""
这些调用中的每一个都只打印前缀。然后控制返回一个级别,并执行以"E“开头的所有输出,依此类推。当控制返回到原始函数时,它将打印出所有可能的结果。
学会递归地“思考”需要练习,但随着时间的推移,这将成为第二天性。
https://stackoverflow.com/questions/1858237
复制相似问题