当前位置:网站首页>【剑指Offer】46. 把数字翻译成字符串
【剑指Offer】46. 把数字翻译成字符串
2022-06-23 16:50:01【LuZhouShiLi】
剑指 Offer 46. 把数字翻译成字符串
题目
给定一个数字,我们按照如下规则把它翻译为字符串:0 翻译成 “a” ,1 翻译成 “b”,……,11 翻译成 “l”,……,25 翻译成 “z”。一个数字可能有多个翻译。请编程实现一个函数,用来计算一个数字有多少种不同的翻译方法。
思路
- 定义f[i]表示前i个数字一共有多少种不同的翻译方法。那么,f[n]就表示前n个数字一共有多少种不同的翻译方法。
- 单独翻译s[i]。由于求的是方案数,如果确定了第i个数字的翻译方式,那么翻译前i个数字和翻译前i - 1个数的方法数就是相同的,即f[i] = f[i - 1]。
- 将s[i]和s[i - 1]组合起来翻译(组合的数字范围在10~25).如果确定了第i个数字和第i - 1个数字的翻译方式,那么翻译前i个数字和翻译前 i-2个数字的翻译方法数就是相同的,即f[i] = f[i - 2];
- 最后将两种决策的方案数加起来,因此状态转移方程就是:f[i] = f[i - 1] + f[i - 2];
代码
class Solution {
public:
int translateNum(int num) {
string s = to_string(num);// 将数字转换为字符串
int n = s.size();
vector<int> f(n + 1);
f[0] = 1;// 初始化
for(int i = 1; i <= n; i++)
{
f[i] = f[i - 1];// 单独翻译s[i]
if(i > 1)
{
int t = (s[i - 2] - '0') * 10 + s[i - 1] - '0';
if(t >= 10 && t <= 25)
{
f[i] += f[i - 2];// 如果符合条件 直接将s[i]和s[i - 1]组合翻译
}
}
}
return f[n];
}
};
边栏推荐
- 手机开户一般哪个证券公司好?在线开户安全么?
- 百度智能云5月产品升级观察站
- CRMEB 二开短信功能教程
- torch学习(一):环境配置
- README
- 论文阅读 (57):2-hydr_Ensemble: Lysine 2-Hydroxyisobutyrylation Identification with Ensemble Method (任务)
- 论文阅读 (58):Research and Implementation of Global Path Planning for Unmanned Surface Vehicle Based...
- 论文阅读 (56):Mutli-features Predction of Protein Translational Modification Sites (任务)
- Latex编译成功但是无法输出到PDF
- 论文阅读 (55):Dynamic Multi-Robot Task Allocation under Uncertainty and Temporal Constraints
猜你喜欢
全局组织结构控制之抢滩登陆
论文阅读 (54):DeepFool: A Simple and Accurate Method to Fool Deep Neural Networks
视频异常检测数据集 (ShanghaiTech)
论文阅读 (53):Universal Adversarial Perturbations
论文阅读 (52):Self-Training Multi-Sequence Learning with Transformer for Weakly Supervised Video Anomaly
对抗攻击与防御 (2):对抗样本的反制策略
论文阅读 (51):Integration of a Holonic Organizational Control Architecture and Multiobjective...
论文阅读 (50):A Novel Matrix Game with Payoffs of Maxitive Belief Structure
console. Log() is an asynchronous operation???
Kdevtmpfsi processing of mining virus -- Practice
随机推荐
- Also using copy and paste to create test data, try the data assistant!
- Async/await
- Programmers are very useful ten tool websites, which are worth collecting
- Troubleshooting and modification process of easycvr interface dislocation in small screen
- Reinforcement learning series (I) -- basic concepts
- Easygbs playback screen is continuously loading. Troubleshooting
- 13. IP address and subnet partitioning (VLSM)
- 12. Manage network environment
- Illustration of mongodb cluster deployment principle (3)
- 6、VLAN
- What if the website is poisoned
- 7、VLAN-Trunk
- Revil - blackmail Virus Emergency Response
- How to make a badge
- Alien world, real presentation, how does the alien version of Pokemon go achieve?
- How to use R language to draw scatter diagram
- How to make validity table
- Ner's past, present and future Overview - Future
- This time, thoroughly understand the SparseArray implementation principle
- Detailed explanation of ssl/tls principle and packet capturing
- Kotlin practical skills you should know
- New function! Qianfan magic pen apaas December capability monthly report
- Nodejs implements multi process
- Goframe framework: basic auth Middleware
- Crmeb second open SMS function tutorial
- Baidu AI Cloud product upgrade Observatory in May
- Which securities company is good for opening a mobile account? Is online account opening safe?
- Kerberoasting without SPN
- Mobile SSH connection tool
- How to make towel washing label
- Establishment and use of SSL VPN (OpenVPN)
- A set of code to launch seven golang web frameworks at the same time
- Vulnerability in McAfee epolicy orchestrator
- Deploy LNMP environment and install Typecho blog
- Cryptography involved in IOT device end
- [learning notes] tidb learning notes (III)
- Goframe framework: add tracing Middleware
- Call face recognition exception
- Self taught programming introduction, what language to learn first?
- How do I write a small program that can automatically edit new year greetings