-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem_STO_0046_translateNum.cc
72 lines (64 loc) · 1.32 KB
/
Problem_STO_0046_translateNum.cc
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
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <string>
#include <vector>
#include "UnitTest.h"
using namespace std;
class Solution
{
public:
// 递归
int process(string &str, int index)
{
if (index == str.length())
{
return 1;
}
int p1 = 0;
if (index + 1 < str.length())
{
// 注意,题设 `a` 是从 0 开始,不是从 `1` 开始
// 也就是说 109 的 09 是不能合并的 !!!
int num = (str[index] - '0') * 10 + (str[index+1] -'0');
p1 = num > 9 && num < 26 ? process(str, index + 2) : 0;
}
int p2 = process(str, index + 1);
return p1 + p2;
}
int translateNum(int num)
{
string str = std::to_string(num);
return process(str, 0);
}
// 递归改动态规划
int dp(int num)
{
string str = std::to_string(num);
int n = str.length();
vector<int> dp(n + 1);
dp[n] = 1;
for (int i = n - 1; i >= 0; i--)
{
dp[i] = dp[i + 1];
if (i + 2 <= n)
{
int num = (str[i] - '0') * 10 + (str[i+1] -'0');
dp[i] += num > 9 && num < 26 ? dp[i + 2] : 0;
}
}
return dp[0];
}
};
void test()
{
Solution s;
EXPECT_EQ_INT(5, s.translateNum(12258));
EXPECT_EQ_INT(2, s.translateNum(25));
EXPECT_EQ_INT(5, s.dp(12258));
EXPECT_EQ_INT(2, s.dp(25));
EXPECT_SUMMARY;
}
int main()
{
test();
return 0;
}