当前位置:网站首页>[jz46 translate numbers into strings]
[jz46 translate numbers into strings]
2022-04-23 14:35:00 【Sugar_ wolf】
describe
There is a way to code letters into numbers :'a'->1, 'b->2', ... , 'z->26'
.
We encode a string into a string of numbers , Then consider reverse compiling into a string .
Because there is no delimiter , Encoding numbers into letters may have a variety of compilation results , for example 11
It can be seen as two 'a'
It can also be seen as a 'k'
. but 10
Can only be 'j'
, because 0
Can't compile into any results .
Now give a string of numbers , Returns the number of possible decoding results
Data range : The string length satisfies 0 < n ≤ 90
Advanced : Spatial complexity O(n), Time complexity O(n)
Example 1
Input : "12"
Return value :2
explain :2 Possible decoding results (”ab” or ”l”)
Example 2
Input : "31717126241541717"
Return value :192
explain :192 Possible decoding results
analysis :
-
First, classify the data , For empty strings 、 The first character is
‘0’
String , as well as‘0’
The previous one is not‘1’
perhaps‘2’
String , Output0
; -
Create a dynamic array
dp[len+1]
, Used to store several decoding results .dp[0]=1,dp[1]=1.
1) Ruodi k Characters and the
k-1
Characters can be combined into a To z Legal characters for , bedp[k+1]=dp[k]+dp[k-1];
2) If it cannot form legal characters , The decoding result does not increase ,
dp[k+1]=dp[k];
3) Ruodi k Characters and the
k-1
There are... In characters‘0’
, A new decoding method cannot be generated ,dp[k+1]=dp[k]
;
Code :
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;
}
}// If 0 In the first place or 0 It's not in front of 1 perhaps 2, Decoding is not possible
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];
}
};
The elapsed time :6ms
exceed 15.56% use C++ Submitted code
Take up memory :424KB
exceed 46.36% use C++ Submitted code
版权声明
本文为[Sugar_ wolf]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231433395837.html
边栏推荐
- Detailed explanation of SAR command
- 8.2 文本预处理
- 查找水仙花数-for循环实践
- Basic regular expression
- source insight via samba
- I thought I could lie down and enter Huawei, but I was confused when I received JD / didi / iqiyi offers one after another
- LLVM - 生成 if-else 以及 PH
- On the insecurity of using scanf in VS
- ASEMI三相整流桥和单相整流桥的详细对比
- API Gateway/API 网关(三) - Kong的使用 - 限流rate limiting(redis)
猜你喜欢
Qt界面优化:Qt去边框与窗体圆角化
顺序表的操作,你真的学会了吗?
API gateway / API gateway (IV) - use of Kong - Integrated JWT and fuse plug-in
线程同步、生命周期
Processing MKDIR: unable to create directory 'AAA': read only file system
C语言知识点精细详解——初识C语言【1】
555定时器+74系列芯片搭建八路抢答器,30s倒计时,附Proteus仿真等
c语言在结构体传参时参数压栈问题
L'externalisation a duré quatre ans.
Man man notes and @ reboot usage of crontab
随机推荐
Branch statement of process control
Ali developed three sides, and the interviewer's set of combined punches made me confused on the spot
全连接层的作用是什么?
8.3 语言模型与数据集
Electronic scale weighing system design, hx711 pressure sensor, 51 single chip microcomputer (proteus simulation, C program, schematic diagram, thesis and other complete data)
爬虫练习题(一)
LotusDB 设计与实现—1 基本概念
Proteus simulation design of DC adjustable regulated power supply (with simulation + paper and other data)
L'externalisation a duré quatre ans.
Man man notes and @ reboot usage of crontab
Matrix exchange row and column
Proteus simulation design of four storey and eight storey elevator control system, 51 single chip microcomputer, with simulation and keil c code
Upgrade of openssh and modification of version number
qt之.pro文件详解
MQ-2和DS18B20的火灾温度-烟雾报警系统设计,51单片机,附仿真、C代码、原理图和PCB等
On the insecurity of using scanf in VS
asp.net使用MailMessage发送邮件的方法
Four ways of SSH restricting login
1分钟看懂执行流程,永久掌握for循环(附for循环案例)
单片机的函数信号发生器,输出4种波形,频率可调,原理图,仿真和C程序