import java.util.Arrays;
import java.util.Comparator;
import org.junit.Test;
public class solution {
@Test
public void testFunc(){
int res = search(22);
System.out.println("res: "+res);
}
//把数字翻译成字符串
/*
* 实现过程:
* f(i)=f(i+1)+g(i,i+1)*f(i+2)
*/
private int[] memo;
public int search(int num1){
String num = num1+"";
memo = new int[num.length()];
Arrays.fill(memo, -1);
memo[num.length()-1]=1;
return searchNum(num,0);
}
public int searchNum(String num, int index){
if (index+1==num.length()) {
return memo[index];
}
if (memo[index]==-1) {
String string=num.substring(index,index+2);
Integer tempNum = Integer.valueOf(string);
int res1=0,res2=0;
res1=searchNum(num, index+1);
if (tempNum<=25 && index+2==num.length()) {
res2=1;
}
if (tempNum<=25 && index+2<num.length()) {
res2=searchNum(num, index+2);
}
memo[index]=res1+res2;
return res1+res2;
}
return memo[index];
}
}
本文介绍了一种将数字转换为字符串的方法,通过递归算法计算可能的字符串组合数量。使用了动态规划的思想来减少重复计算,并通过备忘录模式存储中间结果。此算法适用于解析数字编码,特别当数字代表特定字符时。
1094

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



