https://leetcode.com/problems/decode-ways/#/description
A message containing letters from A-Z is being encoded to numbers using the following mapping:
'A' -> 1 'B' -> 2 ... 'Z' -> 26Given an encoded message containing digits, determine the total number of ways to decode it.
For example, Given encoded message "12", it could be decoded as "AB" (1 2) or "L" (12).
The number of ways decoding "12" is 2.
package go.jacob.day710; /** * 91. Decode Ways(Java) * * @author Jacob * */ public class Demo1 { public int numDecodings(String s) { if (s == null || s.length() == 0) { return 0; } int len = s.length(); int[] res = new int[len + 1]; res[len] = 1; res[len-1] = s.charAt(len - 1) == '0' ? 0 : 1; for (int i = len - 2; i >= 0; i--) { if (s.charAt(i) == '0') continue; else { res[i] = Integer.parseInt(s.substring(i, i + 2)) <= 26 ? res[i + 1] + res[i + 2] : res[i + 1]; } } return res[0]; } }