Decode Ways
问题:
A message containing letters from A-Z is being encoded to numbers using the following mapping:
‘A‘ -> 1 ‘B‘ -> 2 ... ‘Z‘ -> 26
Given 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.
思路:
动态规划
我的代码:
public class Solution { public int numDecodings(String s) { if(s == null || s.length() == 0) return 0; int n = s.length(); int[] dp = new int[n]; if(s.charAt(0) == ‘0‘) return 0; dp[0] = 1; if(n == 1) return 1; if(s.charAt(1) == ‘0‘) { if(isValid(s.substring(0,2))) { dp[1] = 1; } else { return 0; } } else { if(isValid(s.substring(0,2))) { dp[1] = 2; } else { dp[1] = 1; } } for(int i = 2; i < n; i++) { String substr = s.substring(i-1,i+1); if(s.charAt(i) == ‘0‘) { if(!isValid(substr)) return 0; dp[i] = dp[i-2]; continue; } dp[i] = dp[i-1]; if(isValid(substr)) { dp[i] += dp[i-2]; } } return dp[n-1]; } public boolean isValid(String substr) { int sub = Integer.valueOf(substr); if(sub <= 26 && sub >= 10) return true; return false; } }
他人代码:
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; 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()]; } }
学习之处:
原文:http://www.cnblogs.com/sunshisonghit/p/4374762.html