【题目】
规定1和A对应,2和B对应,3和C对应,1-26对应26个字母
那么一个数字字符串比如“111”,就可以转化为“AAA”,或者是“KA”,或者是“KA”
给定一个只有数字组成的额字符串str,问返回有多少种转化结果
public class ConvertToLetterString {
private static int number(String str){
if (null == str || str.length() == 0){
return 0;
}
char[] chars = str.toCharArray();
return process(chars,0);
}
private static int process(char[] chars, int i) {
if (i == chars.length){
return 1;
}
if (chars[i] == '0'){
return 0;
}
if ('1' == chars[i]){
int res = process(chars, i+1);
if (i+1 < chars.length){
res += process(chars, i+2);
}
return res;
}
if ('2' == chars[i]){
int res = process(chars, i+1);
if (i+1 < chars.length && chars[i+1] >= '0' && chars[i+1] <= '6'){
res += process(chars, i+2);
}
return res;
}
return process(chars, i+1);
}
public static void main(String[] args) {
int number = number("1111");
System.out.println(number);
}
}