当前位置:网站首页>Sword finger offer 46 Translate numbers to strings (DP)
Sword finger offer 46 Translate numbers to strings (DP)
2022-06-25 23:12:00 【BugMaker-shen】

The finger of the sword Offer 46. Translate numbers into strings

- The number composed of the current character and the previous character is in the interval [10,25] Inside , The current character can be translated together with the previous character ( Add two characters to the last translation result ), It can also be translated separately ( Add the translation result of the current character to the last translation result ), namely : d p [ i ] = d p [ i − 2 ] + d p [ i − 1 ] dp[i] = dp[i-2]+dp[i-1] dp[i]=dp[i−2]+dp[i−1]
- The number composed of the current character and the previous character is not in the range [10,25] Inside , Only the current character can be translated separately , This is to add the translation result of this character to the result of the last translation , The quantity is the same as the result of the previous translation , namely : d p [ i ] = d p [ i − 1 ] dp[i] = dp[i-1] dp[i]=dp[i−1]
class Solution {
public:
int translateNum(int num) {
string str = to_string(num);
// ans[i] Express str [0, i] There are several ways of translation
vector<int> ans(str.size(), 0);
ans[0] = 1;
for (int i = 1; i < str.size(); i++) {
// Just think ahead of one , because z and 25( Two digit number ) Corresponding
if(str[i - 1] == '1' || (str[i - 1] == '2' && str[i] <= '5')){
// [10, 25] Two digit translation
if (i == 1) {
// The current character can be translated with the previous character ,i=1 when , At this time, the translation results of the previous round will be taken , Causing the visit to cross the border
// there 1 Represents the result of joint translation with the previous character
ans[i] = ans[i - 1] + 1;
continue;
}
ans[i] = ans[i - 1] + ans[i - 2];
}
else {
ans[i] = ans[i - 1];
}
}
return ans[str.size() - 1];
}
};
边栏推荐
- 多模态数据也能进行MAE?伯克利&谷歌提出M3AE,在图像和文本数据上进行MAE!最优掩蔽率可达75%,显著高于BERT的15%...
- C language and the creation and use of database
- Es6-- set
- Es7/es9 -- new features and regularities
- 汇编语言核心要点
- Why can't the mobile phone be used and the computer be connected
- What is 5g? What can 5g do? What will 5g bring in the future?
- 2022 love analysis · panoramic report of it operation and maintenance manufacturers
- Unity技术手册 - 粒子发射和生命周期内速度子模块
- 关闭MongoDB一些服务需要注意的地方(以及开启的相关命令)
猜你喜欢

2022-2028 global co extrusion production line industry research and trend analysis report

百度:2022年十大热度攀升专业出炉,第一名无悬念!

Utilisation de la classe Ping d'Unity

【ModuleBuilder】GP服务实现SDE中两个图层相交选取

OSPF - detailed explanation of GRE tunnel (including configuration command)

Basic concepts of processor scheduling

腾讯《和平精英》新版本将至:新增账号安全保护系统,游戏内违规行为检测升级

2022-2028 global industrial TFT LCD industry survey and trend analysis report

一位博士在华为的22年

Multi modal data can also be Mae? Berkeley & Google proposed m3ae to conduct Mae on image and text data! The optimal masking rate can reach 75%, significantly higher than 15% of Bert
随机推荐
zabbix_server配置文件详解
The difference between synchronize and volatile
Global and Chinese oleic acid operation mode and market supply and demand forecast report 2022 ~ 2028
How to use JMeter for interface testing
电路模块分析练习6(开关)
异或运算符简单逻辑运算 a^=b
1281_FreeRTOS_vTaskDelayUntil实现分析
Several optimization scenarios using like fuzzy retrieval in SQL
The wisdom of questioning? How to ask questions?
元宇宙标准论坛成立
Analysis report on market demand situation and investment direction of China's optical transmission equipment industry from 2022 to 2028
Unity技术手册 - GetKey和GetAxis和GetButton
2022-2028 global extrusion coating and lamination production line industry research and trend analysis report
2022-2028 global industrial touch screen industry research and trend analysis report
Global and Chinese flame retardant ABS industry development trend and market demand analysis report 2022 ~ 2028
2022-2028 global TFT touch screen industry research and trend analysis report
MySQL数据库索引
App new function launch
Es7/es9 -- new features and regularities
字符串变形(字符串大小写切换和变现)