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

边栏推荐
- Common validation comments
- Pytest + allure + Jenkins Environment - - achèvement du remplissage de la fosse
- [matlab] when matrix multiplication in Simulink user-defined function does not work properly, matrix multiplication module in module library can be used instead
- MySQL multi column index (composite index) features and usage scenarios
- C语言二叉树与建堆
- B. Value sequence thinking
- 【数字IC验证快速入门】15、SystemVerilog学习之基本语法2(操作符、类型转换、循环、Task/Function...内含实践练习)
- Linux server development, redis source code storage principle and data model
- C language flight booking system
- 2022 Inner Mongolia latest advanced fire facility operator simulation examination question bank and answers
猜你喜欢

2022年全国最新消防设施操作员(初级消防设施操作员)模拟题及答案

Few shot Learning & meta learning: small sample learning principle and Siamese network structure (I)

Operation suggestions for today's spot Silver

【数字IC验证快速入门】10、Verilog RTL设计必会的FIFO

Niu Mei's mathematical problem --- combinatorial number

探索Cassandra的去中心化分布式架构

Codeforces Global Round 19
![[experience sharing] how to expand the cloud service icon for Visio](/img/42/dba9f78f3fb2049dad8b343b0b36e5.png)
[experience sharing] how to expand the cloud service icon for Visio

Leetcode 40: combined sum II

2022 National latest fire-fighting facility operator (primary fire-fighting facility operator) simulation questions and answers
随机推荐
Quickly use Jacobo code coverage statistics
2022制冷与空调设备运行操作复训题库及答案
Visualization Document Feb 12 16:42
探索干货篇!Apifox 建设思路
[matlab] when matrix multiplication in Simulink user-defined function does not work properly, matrix multiplication module in module library can be used instead
Main window in QT learning 27 application
Bugku CTF daily one question chessboard with only black chess
Value sequence (subsequence contribution problem)
央视太暖心了,手把手教你写HR最喜欢的简历
You Li takes you to talk about C language 6 (common keywords)
太真实了,原来自己一直没有富裕起来是有原因的
buuctf misc USB
贝叶斯定律
Zsh shell adds automatic completion and syntax highlighting
[P2P] local packet capturing
[webrtc] M98 screen and window acquisition
The configuration that needs to be modified when switching between high and low versions of MySQL 5-8 (take aicode as an example here)
有 Docker 谁还在自己本地安装 Mysql ?
Explore dry goods! Apifox construction ideas
SQL优化的魅力!从 30248s 到 0.001s