当前位置:网站首页>LeetCode简单题之找到一个数字的 K 美丽值
LeetCode简单题之找到一个数字的 K 美丽值
2022-07-07 04:47:00 【·星辰大海】
题目
一个整数 num 的 k 美丽值定义为 num 中符合以下条件的 子字符串 数目:
子字符串长度为 k 。
子字符串能整除 num 。
给你整数 num 和 k ,请你返回 num 的 k 美丽值。
注意:
允许有 前缀 0 。
0 不能整除任何值。
一个 子字符串 是一个字符串里的连续一段字符序列。
示例 1:
输入:num = 240, k = 2
输出:2
解释:以下是 num 里长度为 k 的子字符串:
- “240” 中的 “24” :24 能整除 240 。
- “240” 中的 “40” :40 能整除 240 。
所以,k 美丽值为 2 。
示例 2:
输入:num = 430043, k = 2
输出:2
解释:以下是 num 里长度为 k 的子字符串:
- “430043” 中的 “43” :43 能整除 430043 。
- “430043” 中的 “30” :30 不能整除 430043 。
- “430043” 中的 “00” :0 不能整除 430043 。
- “430043” 中的 “04” :4 不能整除 430043 。
- “430043” 中的 “43” :43 能整除 430043 。
所以,k 美丽值为 2 。
提示:
1 <= num <= 10^9
1 <= k <= num.length (将 num 视为字符串)
来源:力扣(LeetCode)
解题思路
简单的方法就是,将给定的num转变为字符串然后再进行遍历判断每个子字符串是否符合条件。在这里需要注意对除以0的处理。
class Solution:
def divisorSubstrings(self, num: int, k: int) -> int:
n,count=str(num),0
for i in range(0,len(n)-k+1):
try:
if not num%int(n[i:i+k]):
count+=1
except:
pass
return count

边栏推荐
- Zsh shell adds automatic completion and syntax highlighting
- Quickly use Jacobo code coverage statistics
- Summary of redis functions
- [UVM foundation] what is transaction
- 微信小程序基本组件使用介绍
- 【数字IC验证快速入门】14、SystemVerilog学习之基本语法1(数组、队列、结构体、枚举、字符串...内含实践练习)
- [Stanford Jiwang cs144 project] lab4: tcpconnection
- Topic not received? Try this
- paddlepaddle 29 无模型定义代码下动态修改网络结构(relu变prelu,conv2d变conv3d,2d语义分割模型改为3d语义分割模型)
- pytest+allure+jenkins环境--填坑完毕
猜你喜欢

Qt学习28 主窗口中的工具栏

【数字IC验证快速入门】15、SystemVerilog学习之基本语法2(操作符、类型转换、循环、Task/Function...内含实践练习)

快速使用 Jacoco 代码覆盖率统计

Operation suggestions for today's spot Silver

Leetcode 40: combined sum II

Figure out the working principle of gpt3

探索干货篇!Apifox 建设思路

You Li takes you to talk about C language 6 (common keywords)

Custom class loader loads network class

Explore dry goods! Apifox construction ideas
随机推荐
[guess-ctf2019] fake compressed packets
Operation suggestions for today's spot Silver
解决问题:Unable to connect to Redis
Téléchargement des données de conception des puces
2022年茶艺师(中级)考试试题及模拟考试
LeetCode 40:组合总和 II
Explore dry goods! Apifox construction ideas
Bugku CTF daily one question chessboard with only black chess
Problem solving: unable to connect to redis
C language communication travel card background system
【数字IC验证快速入门】11、Verilog TestBench(VTB)入门
Qt学习26 布局管理综合实例
CTF daily question day43 rsa5
Introduction to basic components of wechat applet
Implementation of replacement function of shell script
Codeforce c.strange test and acwing
json 数据展平pd.json_normalize
即刻报名|飞桨黑客马拉松第三期等你挑战
pytest+allure+jenkins安装问题:pytest: error: unrecognized arguments: --alluredir
快速使用 Jacoco 代码覆盖率统计