# Decode Ways

Problem:

A message containing letters from A-Z is being encoded to numbers using the following mapping:

'A' → 1

'B' → 2

...

'C' → 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.

f(n) = I(A[0] can be decoded)f(n-1) + I(A[0..1] can be decoded)f(n-2)

 1 class Solution {
2 public:
3     int numDecodings(string s) {
4         // Note: The Solution object is instantiated only once and is reused by each test case.
5         return numDecode(s, 0);
6     }
7
8     int numDecode(const string &s, size_t idx) {
9         if(idx >= s.length()) {
10             return 1;
11         } else {
12             int num1 = 0, num2 = 0;
13             if(s[idx] >= '1' && s[idx] <= '9') {
14                 num1 = numDecode(s, idx+1);
15             }
16
17             if(idx+1 < s.length()) {
18                 int cur = atoi(s.substr(idx,2).c_str());
19                 if(cur >= 10 && cur <= 26) {
20                     num2 = numDecode(s, idx+2);
21                 }
22             }
23
24             return num1 + num2;
25         }
26     }
27 };

 1 class Solution {
2 public:
3     int numDecodings(string s) {
4         // Note: The Solution object is instantiated only once and is reused by each test case.
5         if(s.length() == 0)
6             return 0;
7
8         vector<int> computed(s.length()+1, -1);
9         return numDecode(s, computed, 0);
10     }
11
12     int numDecode(const string &s, vector<int> &computed, size_t idx) {
13         if(idx >= s.length()) {
14             return 1;
15         } else if(computed[idx] >= 0) {
16             return computed[idx];
17         } else {
18             int num1 = 0, num2 = 0;
19             if(s[idx] >= '1' && s[idx] <= '9') {
20                 num1 = numDecode(s, computed, idx+1);
21             }
22
23             if(idx+1 < s.length()) {
24                 int cur = atoi(s.substr(idx,2).c_str());
25                 if(cur >= 10 && cur <= 26) {
26                     num2 = numDecode(s, computed, idx+2);
27                 }
28             }
29
30             computed[idx] = num1 + num2;
31             return num1 + num2;
32         }
33     }
34 };

 1 class Solution {
2 public:
3     int numDecodings(string s) {
4         // Note: The Solution object is instantiated only once and is reused by each test case.
5         if(s.length() == 0 || s[0] == '0')
6             return 0;
7
8         int fn1 = 1, fn2 = 1;
9         for(size_t n = 2; n <= s.length(); ++n) {
10             int f = 0;
11             if(s[n-1] >= '1' && s[n-1] <= '9')
12                 f += fn1;
13
14             if(s[n-2] == '1' || s[n-2] == '2' && s[n-1] <= '6')
15                 f += fn2;
16
17             fn2 = fn1;
18             fn1 = f;
19         }
20
21         return fn1;
22     }
23 };

posted @ 2014-05-02 18:01  夏目家的猫咪老师  阅读(230)  评论(0编辑  收藏  举报