当前位置:网站首页>剑指 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];
}
};
边栏推荐
- Chapter 3 use of requests Library
- 实战:typora里面如何快捷改变字体颜色(博客分享-完美)-2022.6.25(已解决)
- 记|一次exists关键字的学习记录
- 2022-2028 global cloud based remote browser isolation industry research and trend analysis report
- 2022-2028 global iridium electrode industry research and trend analysis report
- Cvpr2022 tutorial | machine learning remote sensing processing: agriculture and food security, University of Maryland
- Unity technical manual - particle foundation main module attributes - upper
- Three layer architecture + routing experiment
- 2022年中职组网络安全新赛题
- Unity的Ping类使用
猜你喜欢

如何用jmeter做接口测试

Why absolute positioning overlaps

2022-2028 global industrial touch screen industry research and trend analysis report

Use of local stack in flask

2022 love analysis · panoramic report of it operation and maintenance manufacturers

The Ping class of unity uses

Nacos source code analysis 01 code structure

Tiger DAO VC产品正式上线,Seektiger生态的有力补充

ES7/ES9 -- 新特性与正则

2022-2028 global iridium electrode industry research and trend analysis report
随机推荐
ADB common commands
Equivalence class, boundary value, application method and application scenario of scenario method
Common MySQL database functions and queries
ES6-Const常量与数组解构
How to use JMeter for interface testing
Yyds dry goods inventory JD 2, why is redis so fast?
Relinearization in homomorphic encryption (ckks)
Reasons why MySQL cannot be connected externally after installing MySQL database on ECs and Solutions
2022-2028 global selective laser sintering service industry research and trend analysis report
Tiger Dao VC products are officially launched, a powerful supplement to seektiger ecology
Data governance is easier said than done
作为一个程序员我们如何快乐的学习成长进步呢?(个人感悟和技术无关)
MySQL数据库索引
Unity technical manual - particle foundation main module attributes - upper
ES6 -- 形参设置初始值、拓展运算符、迭代器、生成函数
Exclusive or operator simple logic operation a^=b
2022-2028 global open source cloud storage industry research and trend analysis report
2022-2028 global cloud based remote browser isolation industry research and trend analysis report
[invitation letter] on March 4, the platform enabled digital intelligence Innovation -- UFIDA BiP PAAS cloud platform IUAP digital intelligence hundred cities forum · Jinan Station
Flex & Bison 开始
