-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecode-ways.cpp
34 lines (34 loc) · 974 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
class Solution {
public:
int numDecodings(string s) {
int n = s.length();
if (n == 0 || s[0] == '0')
return 0;
//circular buffer;
int nd[3] = {1, 1, 0};
int i;
//gather operation
for (i = 2; i <= n ; i++) {
int v = stoi(s.substr(i-2, 2));
if (v == 0){
return 0;
}
else if (v >= 1 && v <= 9){
nd[i%3] = nd[(i-1)%3];
} // 00 01 02 03 04 05 06 07 08 09
else if (v == 10 || v == 20) {
nd[i%3] = nd[(i-2)%3];
}
else if (v >= 11 && v <= 26){
nd[i%3] = nd[(i-1)%3] + nd[(i-2)%3];
}
else if (v % 10 == 0){ // 30 40 50 60 70 80 90
nd[i%3] = 0;
}
else{
nd[i%3] = nd[(i+2)%3];
}
}
return nd[(i-1)%3];
}
};