当前位置: 首页 > news >正文

Leetcode 3704. Count No-Zero Pairs That Sum to N

  • Leetcode 3704. Count No-Zero Pairs That Sum to N
    • 1. 解题思路
    • 2. 代码实现
  • 3704. Count No-Zero Pairs That Sum to N

1. 解题思路

这一题思路上就是一个动态规划的思路,我们只需要考虑每一位上两个数各自取什么非零的位数即可。

唯一需要注意的是,需要注意一下余数以及开头为0的情况即可。

2. 代码实现

给出python代码实现如下:

class Solution:def countNoZeroPairs(self, n: int) -> int:s = str(n)l = len(s)@lru_cache(None)def dp(idx, r, is_num1_empty, is_num2_empty):if idx >= l:return 0 if (r != 0 or is_num1_empty or is_num2_empty) else 1tgt = int(s[idx]) + 10 * rif tgt <= 1 and (not is_num1_empty) and (not is_num2_empty):return 0ans = 0for i in range(min(tgt+1, 10)):j = tgt-iif 0 < i < 10 and 0 < j < 10:ans += dp(idx+1, 0, False, False)elif is_num1_empty and i == 0 and 0 < j < 10:ans += dp(idx+1, 0, True, False)elif is_num2_empty and j == 0 and 0 < i < 10:ans += dp(idx+1, 0, False, True)j = tgt-1-iif 0 < i < 10 and 0 < j < 10:ans += dp(idx+1, 1, False, False)elif is_num1_empty and i == 0 and 0 < j < 10:ans += dp(idx+1, 1, True, False)elif is_num2_empty and j == 0 and 0 < i < 10:ans += dp(idx+1, 1, False, True)elif is_num1_empty and is_num2_empty and i == 0 and j == 0:ans += dp(idx+1, 1, True, True)return ansreturn dp(0, 0, True, True)

提交代码评测得到:耗时73ms,占用内存18.45MB。

http://www.dtcms.com/a/446022.html

相关文章:

  • 微信小程序入门学习教程,从入门到精通,WXSS样式处理语法基础(9)
  • 网站开发技术 文库国外医院网站设计
  • 旅游网站建设的总结深圳市勘察设计
  • 企业网站建设费用的预算西安seo网站关键词
  • jvm垃圾回收算法和垃圾收集器(Serial、Parallel、Parnew、CMS)
  • R 绘图 - 条形图
  • 基于GitHub Copilot的自动化测试流水线
  • MacOS 下 Warp ping 局域网设备报错 ping: sendto: No route to host 的解决方法
  • 网站建设服务标语湖北网站建设搭建
  • reset arp all 概念及题目
  • 如何在 IDEA 中使用 Proguard 自动混淆 Gradle 编译的Java 项目
  • 吉林沈阳网站建设河南互联网公司
  • [人工智能-综述-19]:现在所有使用计算机软件、硬件等技术栈的地方,都将被AI智能体所颠覆和替代
  • 生产架构nginx+spring cloud+redis+mysql+ELFK部署(草稿)
  • 备案网站多少钱镇江市住房与城乡建设部网站
  • 符号运算(华为OD)
  • C++微基础备战蓝桥杯之数组篇10.1
  • 美发店会员管理系统更新
  • HTB Attacking GraphQL Skills Assessment
  • 从化区城郊街道网站麻二村生态建设共青城市建设局网站
  • C# 调用 onnx格式的YOLOv11n模型
  • 使用PyTorch构建你的第一个神经网络
  • 数据结构哈希表--c
  • 腾云公司做网站赣州创可通科技有限公司
  • 大模型预训练深度解析:从基座构建到目标设计
  • 记一次网络io学习流水账
  • 网站建设与推广的实训报告天门网站网站建设
  • vue可以做pc的网站asp网站如何建设
  • YOLO11框架训练高光谱数据归一化问题
  • 宿迁网站定制有什么手机网站