当前位置:网站首页>【JZ46 把数字翻译成字符串】
【JZ46 把数字翻译成字符串】
2022-04-23 14:33:00 【Sugar_wolf】
描述
有一种将字母编码成数字的方式:'a'->1, 'b->2', ... , 'z->26'
。
我们把一个字符串编码成一串数字,再考虑逆向编译成字符串。
由于没有分隔符,数字编码成字母可能有多种编译结果,例如 11
既可以看做是两个 'a'
也可以看做是一个 'k'
。但 10
只可能是 'j'
,因为 0
不能编译成任何结果。
现在给一串数字,返回有多少种可能的译码结果
数据范围:字符串长度满足 0 < n ≤ 90
进阶:空间复杂度 O(n),时间复杂度 O(n)
示例1
输入: "12"
返回值:2
说明:2种可能的译码结果(”ab” 或”l”)
示例2
输入: "31717126241541717"
返回值:192
说明:192种可能的译码结果
分析:
-
首先对数据进行分类,对于空字符串、首字符为
‘0’
的字符串,以及‘0’
的前一位不是‘1’
或者‘2’
的字符串,输出0
; -
建立一个动态数组
dp[len+1]
,用于存储有几种译码结果。dp[0]=1,dp[1]=1。
1)若第k个字符和第
k-1
个字符能够组合成a到z的合法字符,则dp[k+1]=dp[k]+dp[k-1];
2)若无法组成合法字符,则译码结果没有增加,
dp[k+1]=dp[k];
3)若第k个字符和第
k-1
个字符中有‘0’
,则无法产生新的译码方式,dp[k+1]=dp[k]
;
代码:
class Solution {
public:
int solve(string nums) {
int len = nums.size();
if (len == 0)
return 0;
for (int k = 0;k < len;k++)
{
if (nums[k]=='0')
{
if (k == 0)
return 0;
else if (nums[k-1] != '1' && nums[k-1] != '2')
return 0;
}
}//如果0位于第一位或者0的前面不是1或者2,则无法进行译码
int dp[len+1],temp;
dp[0] = 1;
dp[1] = 1;
for (int k=1;k<len;k++)
{
temp=(nums[k]-'0')+(nums[k-1]-'0')*10;
if (temp > 0 && temp <= 26 && nums[k]!= '0'&& nums[k-1] != '0')
dp[k+1] = dp[k]+dp[k-1];
else
dp[k+1] = dp[k];
}
return dp[len];
}
};
运行时间:6ms
超过15.56% 用C++提交的代码
占用内存:424KB
超过46.36%用C++提交的代码
版权声明
本文为[Sugar_wolf]所创,转载请带上原文链接,感谢
https://blog.csdn.net/Sugar_wolf/article/details/124333220
边栏推荐
- API Gateway/API 网关(二) - Kong的使用 - 负载均衡Loadbalance
- API gateway / API gateway (IV) - use of Kong - Integrated JWT and fuse plug-in
- Design of single chip microcomputer Proteus for temperature and humidity monitoring and alarm system of SHT11 sensor (with simulation + paper + program, etc.)
- QT interface optimization: QT border removal and form rounding
- XX project structure notes
- After entering the new company, the operation and maintenance engineer can understand the deployment of the system from the following items
- Redis cluster 原理
- Web page, adaptive, proportional scaling
- 【NLP】HMM隐马尔可夫+维特比分词
- Use cases of the arrays class
猜你喜欢
一款不错的工具:aardio
C语言知识点精细详解——数据类型和变量【1】——进位计数制
AT89C52单片机的频率计(1HZ~20MHZ)设计,LCD1602显示,含仿真、原理图、PCB与代码等
API Gateway/API 网关(二) - Kong的使用 - 负载均衡Loadbalance
After entering the new company, the operation and maintenance engineer can understand the deployment of the system from the following items
常见存储类型和FTP主被动模式解析
Proteus simulation design of four storey and eight storey elevator control system, 51 single chip microcomputer, with simulation and keil c code
Detailed explanation of C language knowledge points -- first knowledge of C language [1]
DVWA之暴力破解(Brute Force)Low-->high
想要成为架构师?夯实基础最重要
随机推荐
pnpm安装使用
Golang 对分片 append 是否会共享数据
KVM learning resources
1 minute to understand the execution process and permanently master the for cycle (with for cycle cases)
关于UDP接收icmp端口不可达(port unreachable)
kprobe 的 3 种使用
ssh限制登录的四种手段
51单片机+LCD12864液晶显示的俄罗斯方块游戏,Proteus仿真、AD原理图、代码、论文等
Branch statement of process control
单相交交变频器的Matlab Simulink建模设计,附Matlab仿真、PPT和论文等资料
四层和八层电梯控制系统Proteus仿真设计,51单片机,附仿真和Keil C代码
DVWA之暴力破解(Brute Force)Low-->high
OpenFaaS实战之四:模板操作(template)
Qt实战:云曦日历篇
DS1302的电子万年历_51单片机,年月日、星期、时分秒、农历和温度,带闹钟,全套资料
基于TLC5615的多路可调数控直流稳压电源,51单片机,含Proteus仿真和C代码等
Parameter stack pressing problem of C language in structure parameter transmission
Four ways of SSH restricting login
Want to be an architect? Tamping the foundation is the most important
矩阵交换行列