当前位置:网站首页>[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-1Characters 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-1There 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
边栏推荐
- Multisim Simulation Design of DC adjustable regulated power supply of LM317 (with simulation + paper + reference)
- Nacos uses demo as configuration center (IV)
- 全连接层的作用是什么?
- 线程同步、生命周期
- Use of ansible and common modules
- C语言知识点精细详解——初识C语言【1】
- Golang 对分片 append 是否会共享数据
- Matrix exchange row and column
- tcp_diag 内核相关实现 1 调用层次
- 利用 MATLAB 编程实现最速下降法求解无约束最优化问题
猜你喜欢

XX project structure notes

Processing MKDIR: unable to create directory 'AAA': read only file system

qt之.pro文件详解

一个月把字节,腾讯,阿里都面了,写点面经总结……

Arduino for esp8266串口功能简介

QT interface optimization: QT border removal and form rounding

Mq-2 and DS18B20 fire temperature smoke alarm system design, 51 single chip microcomputer, with simulation, C code, schematic diagram, PCB, etc

电子秤称重系统设计,HX711压力传感器,51单片机(Proteus仿真、C程序、原理图、论文等全套资料)

C语言知识点精细详解——初识C语言【1】

51单片机+LCD12864液晶显示的俄罗斯方块游戏,Proteus仿真、AD原理图、代码、论文等
随机推荐
Some little records~
Use cases of the arrays class
金九银十,入职字节跳动那一天,我哭了(蘑菇街被裁,奋战7个月拿下offer)
C语言知识点精细详解——初识C语言【1】
成都控制板设计提供_算是详细了_单片机程序头文件的定义、编写及引用介绍
API Gateway/API 网关(二) - Kong的使用 - 负载均衡Loadbalance
【JZ46 把数字翻译成字符串】
Unity_代码方式添加绑定按钮点击事件
交通灯系统51单片机设计(附Proteus仿真、C程序、原理图及PCB、论文等全套资料)
TLC5615 based multi-channel adjustable CNC DC regulated power supply, 51 single chip microcomputer, including proteus simulation and C code
redis的五种数据类型
Detailed explanation of C language knowledge points -- first knowledge of C language [1]
四层和八层电梯控制系统Proteus仿真设计,51单片机,附仿真和Keil C代码
QT actual combat: Yunxi calendar
LLVM - 生成加法
asp.net使用MailMessage发送邮件的方法
flannel 原理 之 子网划分
API Gateway/API 网关(四) - Kong的使用 - 集成Jwt和熔断插件
qt之.pro文件详解
循环队列的基本操作(实验)