Decode Ways

来源:互联网 发布:菜鸟商城源码 编辑:程序博客网 时间:2024/06/03 22:40
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;    }}

0 0