Skip to content

Commit 82ffc9c

Browse files
authored
Create solution.cpp
1 parent be6fc51 commit 82ffc9c

File tree

1 file changed

+30
-0
lines changed
  • 0____剑指offer/dynamic_programming/56.num_to_str

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution
2+
{
3+
public:
4+
int translateNum(int num)
5+
{
6+
std::string str = std::to_string(num);
7+
vector<int> nums(str.size());
8+
nums.clear();
9+
for (auto ch : str)
10+
{
11+
nums.push_back(ch - 0x30);
12+
}
13+
vector<int> dp(str.size()+1);
14+
dp[0] = 1;
15+
dp[1] = 1;
16+
for (int i = 1;i < nums.size();i++)
17+
{
18+
int current = nums[i - 1] * 10 + nums[i];
19+
if (current < 26 && current>9)
20+
{
21+
dp[i + 1] = dp[i] + dp[i - 1];
22+
}
23+
else
24+
{
25+
dp[i + 1] = dp[i];
26+
}
27+
}
28+
return dp.back();
29+
}
30+
};

0 commit comments

Comments
 (0)