|
|
|
@ -36,36 +36,7 @@ public class Code06_ConvertToLetterString {
|
|
|
|
|
return process(str, i + 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static int dpWays2(String s) {
|
|
|
|
|
if (s == null || s.length() == 0) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
char[] str = s.toCharArray();
|
|
|
|
|
int N = str.length;
|
|
|
|
|
int[] dp = new int[N+1];
|
|
|
|
|
dp[N] = 1;
|
|
|
|
|
for(int i = N-1; i >= 0; i--) {
|
|
|
|
|
if (str[i] == '0') {
|
|
|
|
|
dp[i] = 0;
|
|
|
|
|
}
|
|
|
|
|
if (str[i] == '1') {
|
|
|
|
|
dp[i] = dp[i + 1];
|
|
|
|
|
if (i + 1 < str.length) {
|
|
|
|
|
dp[i] += dp[i + 2];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (str[i] == '2') {
|
|
|
|
|
dp[i] = dp[i + 1];
|
|
|
|
|
if (i + 1 < str.length && (str[i + 1] >= '0' && str[i + 1] <= '6')) {
|
|
|
|
|
dp[i] += dp[i + 2]; // (i和i+1)作为单独的部分,后续有多少种方法
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return dp[0];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static int dpWays(String s) {
|
|
|
|
|
public static int dp(String s) {
|
|
|
|
|
if (s == null || s.length() == 0) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
@ -94,8 +65,8 @@ public class Code06_ConvertToLetterString {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
System.out.println(number("11111"));
|
|
|
|
|
System.out.println(dpWays2("11111"));
|
|
|
|
|
System.out.println(number("2132082"));
|
|
|
|
|
System.out.println(dp("2132082"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|