public class {
public int numDecodings(String s) {
if(s.length() == 0 ) {
return 0;
}
int[] count = new int[s.length() + 1];
count[0] = 1;
if(s.charAt(0) != '0') {
count[1] = 1;
}else {
return 0;
}
for(int i = 2; i <= s.length(); i++) {
int first = Integer.parseInt(s.substring(i-1,i));
int second = Integer.parseInt(s.substring(i-2,i));
if(first > 0) {
count[i] += count[i-1];
}
if(second > 9 && second <=26) {
count[i] += count[i-2];
}
}
return count[s.length()];
}
}
近期评论