-
Notifications
You must be signed in to change notification settings - Fork 481
/
0091.cpp
32 lines (29 loc) · 866 Bytes
/
0091.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
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
static int x = []() {std::ios::sync_with_stdio(false); cin.tie(0); return 0; }();
class Solution
{
public:
int numDecodings(string s)
{
if (s.empty()) return 0;
if (s[0] == '0') return 0;
if (s.size() == 1) if (s[0] != '0') return 1;
vector<int> mem(s.size() + 1, 0);
mem[0] = s[0] != '0' ? 1 : 0;
mem[1] = s[1] != '0' ? 1 : 0;
for (unsigned int i = 2; i < s.size() + 1; ++i)
{
int prev = s[i - 1] - '0';
if (prev >= 1 and prev <= 9)
mem[i] += mem[i - 1];
int tail = stoi(s.substr(i-2, 2));
if (tail >= 10 and tail <= 26)
mem[i] += mem[i - 2];
}
return mem[mem.size() - 1];
}
};