[Leetcode 72] 91 Decode Ways
2013-07-22 05:43
323 查看
Problem:
A message containing letters from
Given an encoded message containing digits, determine the total number of ways to decode it.
For example,
Given encoded message
The number of ways decoding
Analysis:
This can be solved by dynamic programming. A new string of length n can be viewed as either a string of length (n-1) plus a new character or a string of length (n-1) plus two new characters. Why only these two ways? Because the longest code of Letter is "26" of length 2. Then the answer a_n can be constructed with the answer of a_{n-1} and a_{n-2}.
For a_{n-1}, the new character must be valid which means it can't be 0. it can expressed as (ch=='0' ? 0 : a_{n-1})
For a_{n-2}, the new sub-string must be valid which means it shou be in the range of 10~26. else return 0;
The initialization should be pay enough attention, the above inductive process requires the current character is 3. So for string of length 0, 1, or 2, we directly compute them.
For string of length 3 or longer, we use the induction. With the initial a_{n-1} and a_{n-2} set to proper values.
a_{n-2} = s[0] == '0' ? 0 : 1;
a_{n-1} = isValid(s[0], s[1], 2) + (isValid(s[0], 1) && isValid(s[1], 1))
Notice the initialization of a_{n-1};
The running time is O(n) and the space needed is O(1).
Code:
View Code
A message containing letters from
A-Zis 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.
Analysis:
This can be solved by dynamic programming. A new string of length n can be viewed as either a string of length (n-1) plus a new character or a string of length (n-1) plus two new characters. Why only these two ways? Because the longest code of Letter is "26" of length 2. Then the answer a_n can be constructed with the answer of a_{n-1} and a_{n-2}.
For a_{n-1}, the new character must be valid which means it can't be 0. it can expressed as (ch=='0' ? 0 : a_{n-1})
For a_{n-2}, the new sub-string must be valid which means it shou be in the range of 10~26. else return 0;
The initialization should be pay enough attention, the above inductive process requires the current character is 3. So for string of length 0, 1, or 2, we directly compute them.
For string of length 3 or longer, we use the induction. With the initial a_{n-1} and a_{n-2} set to proper values.
a_{n-2} = s[0] == '0' ? 0 : 1;
a_{n-1} = isValid(s[0], s[1], 2) + (isValid(s[0], 1) && isValid(s[1], 1))
Notice the initialization of a_{n-1};
The running time is O(n) and the space needed is O(1).
Code:
class Solution { public: int numDecodings(string s) { // Start typing your C/C++ solution below // DO NOT write int main() function if (s.length() == 0) return 0; else if (s.length() == 1) return ck1(s[0], 1); else if (s.length() == 2) { int cnt = 0; if (ck1(s[0], 1) && ck1(s[1], 1)) cnt++; return cnt + ck2(s[0], s[1], 1); } int an2 = ck1(s[0], 1), an1 = ck2(s[0], s[1], 1), an; if (ck1(s[0], 1) && ck1(s[1], 1)) an1++; for (int i=2; i<s.length(); i++) { an = ck1(s[i], an1) + ck2(s[i-1], s[i], an2); an2 = an1; an1 = an; } return an; } int ck1(char c, int n) { return (c == '0') ? 0 : n; } int ck2(char c1, char c2, int n) { if(c1 == '1' || (c1 == '2' && (c2>='0' && c2 <= '6'))) return n; else return 0; } };
View Code
相关文章推荐
- LeetCode(91) Decode Ways
- leetcode 91. Decode Ways
- leetcode 91. Decode Ways
- LeetCode91——Decode Ways
- [leetcode 91] Decode Ways
- Leetcode 91. Decode Ways
- LeetCode 91 Decode Ways
- LeetCode 91 Decode Ways (Python详解及实现)
- LeetCode-91. Decode Ways
- [leetcode 91] Decode Ways
- leetcode_[python/C++]_91_Decode Ways_动态规划
- LeetCode91 Decode Ways
- 【LeetCode】91. Decode Ways
- [LeetCode]--91. Decode Ways(Python + Java)
- 第八周:[leetCode] 91. Decode Ways
- LeetCode||91. Decode Ways
- LeetCode(91) Decode Ways
- 算法学习之动态规划(leetcode 91 Decode Ways)
- leetcode 91. Decode Ways
- [LeetCode72]Populating Next Right Pointers in Each Node