当前位置:网站首页>剑指 Offer 46. 把数字翻译成字符串(DP)
剑指 Offer 46. 把数字翻译成字符串(DP)
2022-06-25 21:57:00 【BugMaker-shen】

- 当前字符和上一个字符组成的数字处于区间[10,25]内,则当前字符可以和上一个字符一起翻译(在上上轮翻译结果上加上两个字符的翻译结果),也可以单独翻译(在上轮翻译结果上加上当前字符的翻译结果),即: 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]
- 当前字符和上一个字符组成的数字不在区间[10,25]内,则只能当前字符单独翻译,就是在上一轮翻译的结果上添加本次字符的翻译结果,数量和上一轮翻译结果相同,即: 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]表示str [0, i]有几种翻译方式
vector<int> ans(str.size(), 0);
ans[0] = 1;
for (int i = 1; i < str.size(); i++) {
// 只需要往前考虑一位,因为z和25(两位数)对应
if(str[i - 1] == '1' || (str[i - 1] == '2' && str[i] <= '5')){
// [10, 25] 可用两位数进行翻译
if (i == 1) {
// 当前字符可以与上一个字符一起翻译,i=1时,此时会取上上轮的翻译结果,导致访问越界
// 这里的1表示和上一个字符联合翻译的结果
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];
}
};
边栏推荐
- Unity technical manual - getKey and getaxis and getbutton
- Which PHP open source works deserve attention
- Global and Chinese oleic acid operation mode and market supply and demand forecast report 2022 ~ 2028
- APP测试要点
- Facing the "industry, University and research" gap in AI talent training, how can shengteng AI enrich the black land of industrial talents?
- 2022-2028 global TFT touch screen industry research and trend analysis report
- Use apiccloud AVM multi terminal component to quickly realize the search function in the app
- Thinking while walking
- Does jQuery cache any selectors- Does jQuery do any kind of caching of “selectors”?
- The Ping class of unity uses
猜你喜欢

Fastjson反序列化随机性失败

2022-2028 global web and browser isolation platform industry research and trend analysis report

Simple and easy-to-use cache library gcache
![[eosio] eos/wax signature error is_ Canonical (c): signature is not canonical](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[eosio] eos/wax signature error is_ Canonical (c): signature is not canonical
This 110 year old "longevity" enterprise has been planning for the next century
2、一个向量乘它的转置,其几何意义是什么?

2022-2028 global variable frequency compressor technology industry research and trend analysis report

Facing the "industry, University and research" gap in AI talent training, how can shengteng AI enrich the black land of industrial talents?
![[WPF] XAML code skills that can be directly used for converting CAD engineering drawings to WPF](/img/a1/09d2dc0ec47c54530da4d42d218d1c.jpg)
[WPF] XAML code skills that can be directly used for converting CAD engineering drawings to WPF

OSPF - detailed explanation of GRE tunnel (including configuration command)
随机推荐
.sql数据库导入错误:/*!40101 SET @[email protected]@COLLATION_CONNECTION */
What do l and R of earphone mean?
ES6-Const常量与数组解构
记|一次exists关键字的学习记录
2022-2028 global web and browser isolation platform industry research and trend analysis report
Thinking while walking
What are the channels for Internet advertising to gain customers?
在线CRUDhasone关联查询报错Unabletouseinternalvariable:List
2022年河南省第一届职业技能大赛网络安全项目试题
2022-2028 global industrial TFT LCD industry survey and trend analysis report
Talk about adapter mode
Initialization process of gstlibav
Unity technical manual - life cycle rotation rotationoverlifetime- speed rotation rotationbyspeed- and external forces
Global and Chinese oleic acid operation mode and market supply and demand forecast report 2022 ~ 2028
2022-2028 global DC linear variable differential transformer (LVDT) industry survey and trend analysis report
Three layer architecture + routing experiment
Talk about adapter mode
MySQL数据库常用函数和查询
Cvpr2022 tutorial | machine learning remote sensing processing: agriculture and food security, University of Maryland
Tiger Dao VC products are officially launched, a powerful supplement to seektiger ecology
