public class Solution {
public int numDecodings(String s) {
if (s == null || s.length() == 0) {
return 0;
}
int[] nums = new int[s.length() + 1];
nums[0] = 1;
nums[1] = s.charAt(0) == '0' ? 0 : 1;
for (int i = 2; i <= s.length(); i++) {
if (s.charAt(i - 1) != '0') {
nums[i] = nums[i - 1];
}
// 得到两位数的方法
int twoDigits = (s.charAt(i - 2) - '0') * 10 + (s.charAt(i - 1) - '0');
if (twoDigits >= 10 && twoDigits <= 26) {
nums[i] += nums[i - 2];
}
}
return nums[s.length()];
}
}
public class Solution {
public int numDecodings(String s) {
if (s == null || s.length() == 0) {
return 0;
}
int prev = 27, digit = 0, single = 1, twoDigit = 1, rst = 0;
for (int i = s.length() - 1; i >= 0; i--) {
digit = s.charAt(i) - '0';
if (digit == 0) {
rst = 0;
} else {
rst = single + (digit * 10 + prev < 27 ? twoDigit : 0);
}
twoDigit = single;
single = rst;
prev = digit;
}
return rst;
}
}