###### 91. 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 a non-empty string containing only digits, determine the total number of ways to decode it.

Example 1:

Input: "12"
Output: 2
Explanation: It could be decoded as "AB" (1 2) or "L" (12).


Example 2:

Input: "226"
Output: 3
Explanation: It could be decoded as "BZ" (2 26), "VF" (22 6), or "BBF" (2 2 6).
class Solution {
public:
int numDecodings(string s) {
if(s.empty() || s.length() == 0) {
return 0;
}
int n = s.length();
vector<int> sum(n+1, 0);
sum[0] = 1;
sum[1] = s[0] != '0' ? 1 : 0;
for(int i = 2; i <= n; i++){
int first = atoi(s.substr(i-1, 1).c_str());
int second = atoi(s.substr(i-2, 2).c_str());
if(first >= 1 && first <= 9){
sum[i] += sum[i-1];
}
if(second >=10 && second <= 26){
sum[i] += sum[i-2];
}
}
return sum[n];
}
};

#### Leetcode 91. Decode Ways 路线解析 解题报告

2016-07-08 20:31:55

#### LeetCode 91. Decode Ways（解码方法）

2016-05-23 00:42:32

#### LeetCode 91 Decode Ways (Python详解及实现)

2017-08-10 10:18:48

#### [Leetcode]91. Decode Ways@python

2016-01-23 18:38:34

#### 91. Decode Ways | Java最短代码实现

2016-03-22 20:39:39

#### leetCode 91.Decode Ways (解码方式) 解题思路和方法

2015-07-27 09:49:37

#### LeetCode91:Decode Ways

2015-06-18 18:17:39

#### leetcode 91 c++. Decode Ways

2016-12-14 10:48:59

#### LeetCode91——Decode Ways

2016-03-02 23:08:37

#### Leetcode Decode Ways 解题报告

2014-02-25 23:06:03

91. Decode Ways