当前位置:网站首页>剑指 Offer 46. 把数字翻译成字符串
剑指 Offer 46. 把数字翻译成字符串
2022-07-23 08:03:00 【ATTACH_Fine】
题目
给定一个数字,我们按照如下规则把它翻译为字符串:0 翻译成 “a” ,1 翻译成 “b”,……,11 翻译成 “l”,……,25 翻译成 “z”。一个数字可能有多个翻译。请编程实现一个函数,用来计算一个数字有多少种不同的翻译方法。
示例:
思路

代码
class Solution {
public int translateNum(int num) {
// 设动态规划列表 dp[i] 代表以 x_i为结尾的数字的翻译方案数量。
String str = String.valueOf(num);
int len = str.length();
int[] dp = new int[len+1];
dp[0] = 1;
dp[1] = 1;
for(int i = 2; i <= len; i++){
String temp = str.substring(i-2,i);
if(temp.compareTo("10") >= 0 && temp.compareTo("25") <= 0){
dp[i] = dp[i-1] + dp [i-2];
}else
dp[i] = dp[i-1];
}
return dp[len];
}
}
边栏推荐
- Notes on the fourth day
- rtx3080相当于gtx什么显卡 rtx3080显卡什么水平 rtx3080显卡怎么样
- JS数据类型判断方式总结
- 考研题库小程序中如何实现打开考研思维导图pdf
- 达人评测 酷睿i9 12950hx和i9 12900hx区别哪个强
- Fabric.js 基础笔刷
- ThreadLocal interview Kills 11 consecutive questions
- Golang remote server debugging
- How many processors is Tianji 1100 equivalent to snapdragon? How about Tianji 1100 equivalent to snapdragon
- 多重背包!
猜你喜欢
随机推荐
第四天笔记
Where does pytorch work?
How about the performance of Intel Celeron 7305? What level is it equivalent to
【百企行】牛耳教育助力高校访企拓岗促就业专项行动
小米12S Pro和小米12Pro天玑版区别 两者配置对比
How to open the thought map pdf of postgraduate entrance examination in the small program of postgraduate entrance examination question bank
Excitation generator, monitor
Description of test platform and hardware design
NOTICE: PHP message: PHP Warning: PHP Startup: Unable to load dynamic library ‘*****‘
BGP联邦实验
Day 12 notes
天玑720相当于骁龙多少处理器 天玑720相当于骁龙多少 天玑720怎么样
第六天笔记
Day 8 notes
Principle of container network
酷睿i7 1165g7相当于什么水平 i71165g7属于哪个档次
Remember that a vulnhub target plane exercise successfully won the root permission
天玑820相当于骁龙多少处理器 天玑1100相当于骁龙多少 天玑820怎么样
激励发生器、监测器
赛扬n5095处理器怎么样 英特尔n5095核显相当于什么水平







