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' ? 1 : 0;
int first = 1;
int second = s.charAt(0) != '0' ? 1: 0;
if (s.length() == 1) {
return second;
}
int third = 0;
for (int i = 2; i <= s.length(); i++) {
third = 0;
if (s.charAt(i - 1) != '0') {
//nums[i] = nums[i - 1];
third = second;
}
int sum = (s.charAt(i - 2) - '0') * 10 + (s.charAt(i - 1) - '0');
if (sum >= 10 && sum <= 26) {
//nums[i] += nums[i - 2];
third += first;
}
first = second;
second = third;
}
return third;
}
}
Decode Ways
最新推荐文章于 2021-02-16 21:52:55 发布
