-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecode_Ways.cpp
More file actions
71 lines (60 loc) · 1.46 KB
/
Decode_Ways.cpp
File metadata and controls
71 lines (60 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Source : https://oj.leetcode.com/problems/decode-ways/
// Author : zheng yi xiong
// Date : 2015-01-05
/**********************************************************************************
*
* 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.
*
**********************************************************************************/
#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int numDecodings(string s) {
if (s.empty() || ('0' == s[0]) )
{
return 0;
}
int len = s.size();
vector<int> vec_decode(len + 1, 0);
vec_decode[0] = 1;
vec_decode[1] = 1;
for (int i = 1; i < len; ++i)
{
if ('0' == s[i])
{
if ('0' == s[i - 1] || '2' < s[i - 1])
{
return 0;
}
vec_decode[i + 1] = vec_decode[i - 1];
}
else if ( ('1' == s[i - 1]) || ('2' == s[i - 1] && '6' >= s[i]) )
{
vec_decode[i + 1] = vec_decode[i] + vec_decode[i - 1];
}
else
{
vec_decode[i + 1] = vec_decode[i];
}
}
return vec_decode[len];
}
};
int _tmain(int argc, _TCHAR* argv[])
{
string s = "1212";
Solution so;
int numDecode = so.numDecodings(s);
return 0;
}