当前位置:网站首页>leetcode:639. 解码方法 II
leetcode:639. 解码方法 II
2022-08-02 08:49:00 【OceanStar的学习笔记】
题目来源
题目描述



class Solution {
public:
int numDecodings(string s) {
}
};
题目解析
class Solution {
int process(string str, int i){
if(i == str.size()){
return 1;
}
if(str[i] == '0'){
return 0;
}
// str[index]有字符且不是'0'
if(str[i] != '*'){
// str[index] = 1~9
int p1 = process(str, i + 1);
if(i + 1 == str.size()){
return p1;
}
if(str[i + 1] != '*'){
int num = (str[i] - '0') * 10 + str[i + 1] - '0';
int p2 = 0;
if(num < 27){
p2 = process(str, i + 2);
}
return p1 + p2;
}
// str[i+1] == '*'
// i i+1 -> 一起转 1* 2* 3* ...9*
int p2 = 0;
if(str[i] < '3'){
p2 = process(str, i + 2) * (str[i] == '1' ? 9 : 6);
}
return p1 + p2;
}
// str[i] == '*' 1~9
int p1 = 9 * process(str, i + 1); // i 单转 9种
if(i + 1 == str.size()){
return p1;
}
if(str[i + 1] != '*'){
// * 0 10 20
// * 1 11 21
// * 2 12 22
// * 3 13 23
// * 6 16 26
// * 7 17
// * 8 18
// * 9 19
int p2 = (str[i + 1] < '7' ? 2 : 1) * process(str, i + 2);
return p1 + p2;
}else{
// **
// 11~19 9
// 21 ~26 6
// 15
int p2 = 15 * process(str, i + 2);
return p1 + p2;
}
}
public:
int numDecodings(string s) {
return process(s, 0);
}
};
边栏推荐
猜你喜欢

openpyxl 单元格合并

每天花2小时恶补腾讯T8纯手打688页SSM框架和Redis,成功上岸美团

【论文阅读】Distilling the Knowledge in a Neural Network
![Three types of [OC learning notes] Block](/img/40/edf59e6e68891ea7c9ab0481fe7bfc.png)
Three types of [OC learning notes] Block

HCIP笔记第十三天

C语言基础_共用体

三维体尺测量

OneNote Tutorial, How to Create More Spaces in OneNote?

Jetpack Compose 中的状态管理
![Detailed explanation of calculation commands in shell (expr, (()), $[], let, bc )](/img/3c/5cc4d16b9b525997761445f32802d5.png)
Detailed explanation of calculation commands in shell (expr, (()), $[], let, bc )
随机推荐
cococreator 动态设置精灵
动态规划每日一练(3)
Jenkins--基础--6.2--Pipeline--语法--声明式
三维体尺测量
二分类和多分类
三国演义小说
不用Swagger,那我用啥?
tf.where使用
测试时大量TIME_WAIT
shell中计算命令详解(expr、(())、 $[]、let、bc )
C语言基础_结构体
Flink 监控指南 被动拉取 Rest API
MySQL ODBC驱动简介
自定义View实现波浪荡漾效果
EPSANet: An Efficient Pyramid Split Attention Block on Convolutional Neural Network
【微信小程序】本地服务页面案例实现
pnpm的安装与使用
LeetCode_2357_使数组种所有元素都等于零
What is the function of the import command of the page directive in JSP?
Analysis of software testing technology How far is Turing test from us