当前位置:网站首页>剑指 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];
}
}
边栏推荐
- js 实现 encode64 加密
- 拖拽----
- Is machine learning difficult to get started? Tell me how I started machine learning quickly!
- Rip experiment
- Day108. Shang Yitong: interface docking of hospital simulation system - query of hospital | Department | shift scheduling, addition, deletion, modification and paging conditions
- How to judge whether an object is empty
- Pbootcms数据库转换教程(sqlite转mysql详细教程)
- pingbanceshi
- MySQL enables scheduled task execution
- fastadmin更改默认表格按钮的弹窗大小
猜你喜欢

Notes on the fourth day

Day 11 notes

kafka消费报错coordinator unavailable.Rediscovery will be attempt redisCovery

581. 最短无序连续子数组

rtx3070ti显卡什么水平 rtx3070ti显卡什么级别 rtx3070ti显卡怎么样

MGRE comprehensive experiment

STM32 output sine wave +cubemx configuration +hal Library

How many processors is Tianji 1100 equivalent to snapdragon? How about Tianji 1100 equivalent to snapdragon

How about the nuclear display performance of Ruilong R7 Pro 6850h? What level is it equivalent to

第六天笔记
随机推荐
过程块和方法
Using redis to realize distributed lock (single redis)
Stream stream is used for classification display.
What level is the notebook core i5 1135g7 equivalent to? How about i5 1135g7 performance
iQOO 10 Pro和vivo X80 Pro区别 哪个好详细参数配置对比
SDF refraction and reflection effect recording
ThreadLocal 面试夺命11连问
数据库连接池 & DBUtils
网络安全笔记1——Internet协议的安全性
js 实现随机生成UUID
接口interface
记一次Vulnhub靶机练习成功拿下root权限
子序列 --- 编辑距离
Canvas eraser function
rtx3090ti什么水平 rtx3090ti显卡什么级别 rtx3090ti显卡怎么样
将我理解的web3.0讲给你听
How about the performance of Ruilong R7 Pro 5875u? What level is it equivalent to
How about the performance of Intel Celeron 7305? What level is it equivalent to
Day 5 experiment
Rtx3080 is equivalent to GTX. What kind of graphics card is rtx3080? What level is rtx3080