-
Notifications
You must be signed in to change notification settings - Fork 0
/
91.cpp
43 lines (34 loc) Β· 1.06 KB
/
91.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
#include <iostream>
#include <string>
using namespace std;
int numDecodings(string s) {
int* dp = new int[s.size()+2];
for(int i = 0; i < s.size()+2; i++) {
// 0λ²μ§Έ 1λ²μ§ΈμΌ κ²½μ° κ²½μ°μ μ 1κ°
if(i < 2) {
dp[i] = 1;
// κ°μκ° 2κ°μΌ λ λΆν° λΉκ΅
} else {
int value = s[i-2] -'0';
// κ°λ¦¬ν€κ³ μλ μκ° 1~9μ΄λ©΄
if(value > 0 && value <= 9) {
// μμ κ²½μ°μ μμ λμΌ
dp[i] += dp[i-1];
}
// λλ²μ§Έ μ΄μμ λ¬Έμλ₯Ό κ°λ¦¬ν€κ³ μλ€λ©΄
if(i > 2) {
// μ΄μ λ¬Έμμ λν κ°μ μΆμΆ
int num = stoi(s.substr(i-3, i-1));
// μ΄μ λ¬Έμκ° 10~26 λ²μμ μλ€λ©΄ 쑰건 λ§μ‘±
if(num >= 10 && num <= 26) {
dp[i] += dp[i-2];
}
}
}
}
return dp[s.size()+1];
}
int main(void) {
string s = "12";
cout << numDecodings(s) << '\n';
}