解题思路
dfs
代码
class Solution {
int res = 0;
void dfs(String str, int n) {
if (n >= str.length() - 1) {
res++;
return;
}
dfs(str, n + 1);//不与后一位组合
if (str.charAt(n) - '0' > 0 && n + 1 < str.length() && (str.charAt(n) - '0') * 10 + str.charAt(n + 1) - '0' < 26) {
dfs(str, n + 2);//与后一位组合,需要满足不超过26,且不超过总长度
}
}
public int translateNum(int num) {
res = 0;
String str = String.valueOf(num);
dfs(str, 0);
return res;
}
}