-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdecode_ways.cpp
43 lines (34 loc) · 931 Bytes
/
decode_ways.cpp
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
class Solution {
public:
int numDecodings(string s) {
int n = s.size();
int total = 1;
int single_count = 1;
if(s[0]=='0')
return 0;
for(int i=n-2; i>=0; i--)
{
if(s[i+1]=='0' and (s[i]>'2'|| s[i]=='0'))
return 0;
if(s[i]=='0')
continue;
if(s[i+1]=='0')
{
single_count=0;
continue;
}
if(s[i]>'2' || s[i+1]>'6' and s[i]=='2')
{
single_count = total;
continue;
}
else
{
int temp = total;
total = total+single_count;
single_count = single_count + (temp-single_count);
}
}
return total;
}
};