当前位置:网站首页>[sword finger offer] 46 Translate numbers into strings
[sword finger offer] 46 Translate numbers into strings
2022-06-23 18:15:00 【LuZhouShiLi】
The finger of the sword Offer 46. Translate numbers into strings
subject
Given a number , We translate it as a string according to the following rules :0 Translate into “a” ,1 Translate into “b”,……,11 Translate into “l”,……,25 Translate into “z”. A number may have more than one translation . Please program a function , Used to calculate how many different translation methods a number has .
Ideas
- Definition f[i] Before presentation i How many different translation methods are there for a number . that ,f[n] Just before n How many different translation methods are there for a number .
- Separate translation s[i]. Because we are looking for the number of schemes , If the second... Is determined i The translation of numbers , So before translation i A number and before translation i - 1 The number of methods is the same , namely f[i] = f[i - 1].
- take s[i] and s[i - 1] Combine to translate ( The combined numbers range from 10~25). If the second... Is determined i The first number and the second number i - 1 The translation of numbers , So before translation i A number and before translation i-2 The number of translation methods for numbers is the same , namely f[i] = f[i - 2];
- Finally, add up the number of schemes of the two decisions , So the state transition equation is :f[i] = f[i - 1] + f[i - 2];
Code
class Solution {
public:
int translateNum(int num) {
string s = to_string(num);// Convert numbers to strings
int n = s.size();
vector<int> f(n + 1);
f[0] = 1;// initialization
for(int i = 1; i <= n; i++)
{
f[i] = f[i - 1];// Separate translation 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];// If you qualify Direct will s[i] and s[i - 1] Combinatorial translation
}
}
}
return f[n];
}
};
边栏推荐
- This time, thoroughly understand the SparseArray implementation principle
- Intelligent supply chain collaborative management solution for logistics industry
- 科技互动沙盘是凭借什么收获人气的
- Goframe framework: add tracing Middleware
- What if the website is poisoned
- How to make validity table
- How to design a seckill system - geek course notes
- 聊一聊数据库的行存与列存
- New function! Qianfan magic pen apaas December capability monthly report
- csdn涨薪秘籍之Jenkins集成allure测试报告全套教程
猜你喜欢
![[unity] instructions for beginners of textanimator plug-in](/img/aa/a406c70a28931ac138e65787a0aabd.png)
[unity] instructions for beginners of textanimator plug-in

【华中科技大学】考研初试复试资料分享

【win10 VS2019 opencv4.6 配置参考】

论文阅读 (53):Universal Adversarial Perturbations

【ESP8266-01s】獲取天氣,城市,北京時間

Crmeb second open SMS function tutorial

科技互动沙盘是凭借什么收获人气的

Paper reading (56):muti features predction of protein translational modification sites (task)
![[Wwise] there is no sound problem after Wwise is embedded in unity and packaged](/img/70/4131671f5dfd36324cbe9bacea6ac4.png)
[Wwise] there is no sound problem after Wwise is embedded in unity and packaged

Customer service system building tutorial_ Installation and use mode under the pagoda panel_ Docking with official account_ Support app/h5 multi tenant operation
随机推荐
百度智能云5月产品升级观察站
Theory of technology that must be learned by chip manufacturers (4-1) clock technology and reset Technology
org. apache. ibatis. binding. BindingException: Invalid bound statement (not found):...
Reinforcement learning series (I) -- basic concepts
Wiley-中国科学院文献情报中心开放科学联合研讨会第二讲:开放获取期刊选择及论文投稿...
[websocket] knowledge points for developing online customer service system meaning of status code returned by websocket
Prevent users from submitting repeatedly in the uniapp project
. Net cloud native architect training camp (responsibility chain mode) -- learning notes
论文阅读 (50):A Novel Matrix Game with Payoffs of Maxitive Belief Structure
Thesis reading (57):2-hydr_ Ensemble: lysine 2-hydroxyisobutyrylation identification with ensemble method (task)
esp8266-01s 不能连接华为路由器解决方法
信用卡产品开发周期从23周缩短至9周,银行运维组织如何转向敏捷?
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):...
【win10 VS2019 opencv4.6 配置参考】
Paper reading (49):big data security and privacy protection (Kopp)
Paper reading (54):deepfool: a simple and accurate method to four deep neural networks
[WebSocket] 开发在线客服系统知识点-websocket返回状态码的含义
【C工具】------点阵模拟测试2
The draganddrop framework, a new member of jetpack, greatly simplifies the development of drag and drop gestures!
论文阅读 (57):2-hydr_Ensemble: Lysine 2-Hydroxyisobutyrylation Identification with Ensemble Method (任务)