-
Notifications
You must be signed in to change notification settings - Fork 0
/
639. Decode Ways II.cpp
60 lines (59 loc) · 1.79 KB
/
639. Decode Ways II.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
class Solution {
public:
int numDecodings(string s) {
if(s.empty() || s.front() == '0') return 0;
int mod = pow(10, 9) + 7;
long p1 = (s[0] == '*') ? 9 : 1, p2 = 1, t;
for(int i = 1; i < s.size(); i++){
if(s[i] == '0') p1 = 0;
if(s[i - 1] != '*' && s[i] != '*'){
if(s[i - 1] == '1' || s[i - 1] == '2' && s[i] < '7'){
p1 = p1 + p2;
p2 = p1 - p2;
}
else p2 = p1;
}
else{
if(s[i - 1] == '*' && s[i] == '*'){
t = p1;
p1 = p1 * 9 + p2 * (9 + 6);
p2 = t;
}
else if(s[i] == '*'){
if(s[i - 1] == '1'){
t = p1;
p1 = p1 * 9 + p2 * 9;
p2 = t;
}
else if(s[i - 1] == '2'){
t = p1;
p1 = p1 * 9 + p2 * 6;
p2 = t;
}else{
t = p1;
p1 = p1 * 9;
p2 = t;
}
}
else{
if(s[i] == '0'){
t = p1;
p1 = p2 * 2;
p2 = t;
}
else if(s[i] < '7'){
t = p1;
p1 = p1 + p2 * 2;
p2 = t;
}
else{
p1 = p1 + p2;
p2 = p1 - p2;
}
}
}
p1 = p1 % mod;
}
return p1;
}
};