当前位置:网站首页>[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
边栏推荐
- 555定时器+74系列芯片搭建八路抢答器,30s倒计时,附Proteus仿真等
- 【NLP】HMM隐马尔可夫+维特比分词
- Unity_代码方式添加绑定按钮点击事件
- 电子秤称重系统设计,HX711压力传感器,51单片机(Proteus仿真、C程序、原理图、论文等全套资料)
- 自动化的艺术
- 外包幹了四年,廢了...
- AT89C51单片机的数字电压表开发,量程0~5V,proteus仿真,原理图PCB和C程序等
- flannel 原理 之 TUN模式
- I thought I could lie down and enter Huawei, but I was confused when I received JD / didi / iqiyi offers one after another
- TLS/SSL 协议详解 (30) SSL中的RSA、DHE、ECDHE、ECDH流程与区别
猜你喜欢
Electronic perpetual calendar of DS1302_ 51 single chip microcomputer, month, day, week, hour, minute and second, lunar calendar and temperature, with alarm clock and complete set of data
Tongxin UOS uninstall php7 2.24, install php7 4.27 ; Uninstall and then install PHP 7.2.34
51单片机+LCD12864液晶显示的俄罗斯方块游戏,Proteus仿真、AD原理图、代码、论文等
顺序栈的基本操作
51单片机的直流电机PWM调速控制系统(附Proteus仿真+C程序等全套资料)
flannel 原理 之 TUN模式
C语言知识点精细详解——初识C语言【1】——你不能不知的VS2022调试技巧及代码实操【2】
电子秤称重系统设计,HX711压力传感器,51单片机(Proteus仿真、C程序、原理图、论文等全套资料)
C语言知识点精细详解——数据类型和变量【2】——整型变量与常量【1】
1N5408-ASEMI整流二极管1N5408
随机推荐
Detailed explanation of C language P2 selection branch statement
The initial C language framework is suitable for review and preliminary understanding
阿里研发三面,面试官一套组合拳让我当场懵逼
AT89C51 MCU digital voltmeter development, measuring range 0 ~ 5V, proteus simulation, schematic diagram, PCB and C program, etc
C语言知识点精细详解——初识C语言【1】——你不能不知的VS2022调试技巧及代码实操【2】
OpenFaaS实战之四:模板操作(template)
Mq-2 and DS18B20 fire temperature smoke alarm system design, 51 single chip microcomputer, with simulation, C code, schematic diagram, PCB, etc
Master in minutes --- ternary operator (ternary operator)
PWM speed regulation control system of DC motor based on 51 single chip microcomputer (with complete set of data such as Proteus simulation + C program)
DVWA之暴力破解(Brute Force)Low-->high
基于单片机的DS18B20的数字温度监控报警系统设计【LCD1602显示+Proteus仿真+C程序+论文+按键设置等】
Usage of BC
八路抢答器系统51单片机设计【附Proteus仿真、C程序、原理图及PCB文件、元器件清单和论文等】
循环队列的基本操作,你学会了吗?
MySQL报错packet out of order
asp.net使用MailMessage发送邮件的方法
全连接层的作用是什么?
矩阵交换行列
Upgrade of openssh and modification of version number
Basic regular expression