当前位置:网站首页>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);
}
};
边栏推荐
猜你喜欢
随机推荐
JS中的数组方法
A little bit of knowledge - why do not usually cook with copper pots
RestTemlate源码分析及工具类设计
Pycharm (1) the basic use of tutorial
next permutation
js函数防抖和函数节流及其使用场景
在 QT Creator 上配置 opencv 环境的一些认识和注意点
堪称神级的阿里巴巴“高并发”教程《基础+实战+源码+面试+架构》
文章解读 -- FlowNet3D:Learning Scene Flow in 3D Point Clouds
What is the function of the import command of the page directive in JSP?
PyCharm使用教程(较详细,图+文)
shell中计算命令详解(expr、(())、 $[]、let、bc )
AttributeError: module ‘clr‘ has no attribute ‘AddReference‘
Openwrt_树莓派B+_Wifi中继
Flink 系统性学习笔记系列
PyQt5 (a) PyQt5 installation and configuration, read from the folder and display images, simulation to generate the sketch image
pnpm的安装与使用
普林斯顿微积分读本03第二章--编程实现函数图像绘制、三角学回顾
Gorilla Mux 和 GORM 的使用方法
Three types of [OC learning notes] Block