当前位置:网站首页>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

边栏推荐
- 2022年全国最新消防设施操作员(初级消防设施操作员)模拟题及答案
- Linux server development, MySQL transaction principle analysis
- Linux server development, MySQL cache strategy
- Most elements
- The element with setfieldsvalue set is obtained as undefined with GetFieldValue
- JSON data flattening pd json_ normalize
- Roulette chart 2 - writing of roulette chart code
- [guess-ctf2019] fake compressed packets
- [UTCTF2020]file header
- Regular e-commerce problems part1
猜你喜欢

Linux server development, detailed explanation of redis related commands and their principles

Hands on deep learning (IV) -- convolutional neural network CNN

Qt学习26 布局管理综合实例

The charm of SQL optimization! From 30248s to 0.001s

2022 recurrent training question bank and answers of refrigeration and air conditioning equipment operation

Bugku CTF daily one question chessboard with only black chess

Open source ecosystem | create a vibrant open source community and jointly build a new open source ecosystem!

2022 simulated examination question bank and online simulated examination of tea master (primary) examination questions

buuctf misc USB

2022制冷与空调设备运行操作复训题库及答案
随机推荐
2022 recurrent training question bank and answers of refrigeration and air conditioning equipment operation
C语言二叉树与建堆
Info | webrtc M97 update
Explore dry goods! Apifox construction ideas
Leanote private cloud note building
mysql多列索引(组合索引)特点和使用场景
【数字IC验证快速入门】15、SystemVerilog学习之基本语法2(操作符、类型转换、循环、Task/Function...内含实践练习)
开源生态|打造活力开源社区,共建开源新生态!
贝叶斯定律
Force buckle 144 Preorder traversal of binary tree
Why should we understand the trend of spot gold?
【数字IC验证快速入门】12、SystemVerilog TestBench(SVTB)入门
Linux server development, SQL statements, indexes, views, stored procedures, triggers
Codeforces Global Round 19
Linux server development, MySQL transaction principle analysis
Pytorch(六) —— 模型调优tricks
These five fishing artifacts are too hot! Programmer: I know, delete it quickly!
Record a stroke skin bone error of the skirt
[SUCTF 2019]Game
Zhilian + AV, AITO asked M7 to do more than ideal one