forked from daiwb/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumDecodings.cpp
More file actions
41 lines (38 loc) · 1011 Bytes
/
numDecodings.cpp
File metadata and controls
41 lines (38 loc) · 1011 Bytes
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
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define RFOR(i,a,b) for(int i=(a);i>=(b);--i)
typedef long long LL;
class Solution {
public:
int numDecodings(string s) {
int n = s.length();
if (n == 0) return 0;
REP(i,n) {
if (!(s[i] >= '0' && s[i] <= '9')) return 0;
}
vector<int> mm(n + 5, 0);
mm[n] = 1;
RFOR(i,n-1,0) {
if (s[i] == '0') {
mm[i] = 0;
continue;
}
mm[i] = mm[i + 1];
if (i + 1 < n) {
int num = (s[i] - '0') * 10 + s[i + 1] - '0';
if (num <= 26) mm[i] += mm[i + 2];
}
}
return mm[0];
}
};
int main() {
Solution s;
cout << s.numDecodings("12") << endl;
cout << s.numDecodings("10") << endl;
return 0;
}