当前位置:网站首页>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);
}
};
边栏推荐
- Golang ORM框架 — GORM
- MySQL ODBC驱动简介
- 【特别提醒】订阅此专栏的用户请先阅读本文再决定是否需要购买此专栏
- 线程池的使用及ThreadPoolExecutor源码分析
- High imitation [Huawei consumer business official website] and wonderful animation analysis: practice embedding JS code in low-code platform
- Redisson报异常attempt to unlock lock, not locked by current thread by node id解决方案
- Openwrt_树莓派B+_Wifi中继
- redis-desktop-manager下载安装
- Fiddler(七) - Composer(组合器)克隆或者修改请求
- Jetpack Compose 中的状态管理
猜你喜欢

“蔚来杯“2022牛客暑期多校训练营4

PostgreSQL learning summary (11) - PostgreSQL commonly used high-availability cluster solutions

MySQL安装与卸载详细教程

Jenkins--基础--07--Blue Ocean

普林斯顿微积分读本03第二章--编程实现函数图像绘制、三角学回顾

Openwrt_树莓派B+_Wifi中继

PyQt5 (a) PyQt5 installation and configuration, read from the folder and display images, simulation to generate the sketch image

软件exe图标变记事本或浏览器、360压缩打不开的几种应急解决方法

OneNote Tutorial, How to Create More Spaces in OneNote?

Redisson报异常attempt to unlock lock, not locked by current thread by node id解决方案
随机推荐
Mysql Mac版下载安装教程
三维体尺测量
【Flink 问题】Flink 如何提交轻量jar包 依赖该如何存放 会遇到哪些问题
普林斯顿微积分读本03第二章--编程实现函数图像绘制、三角学回顾
“蔚来杯“2022牛客暑期多校训练营4
Docker内MySQL主从复制学习,以及遇到的一些问题
堪称神级的阿里巴巴“高并发”教程《基础+实战+源码+面试+架构》
【C】关于柔性数组.简要的谈谈柔性数组
二分类和多分类
十、 网络管理
Detailed explanation of calculation commands in shell (expr, (()), $[], let, bc )
HCIA动态主机配置协议实验(dhcp)
postman使用方法
day——05 迭代器,生成器
Golang ORM框架 — GORM
抓包工具Charles修改Response步骤
JSP页面中page指令有哪些属性及方法可使用呢?
MySQL ODBC驱动简介
day_05 time 模块
主流监控系统工具选型及落地场景参考