当前位置:网站首页>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
边栏推荐
- Pytorch(六) —— 模型调优tricks
- Qt学习27 应用程序中的主窗口
- [matlab] when matrix multiplication in Simulink user-defined function does not work properly, matrix multiplication module in module library can be used instead
- php导出百万数据
- [quick start of Digital IC Verification] 17. Basic grammar of SystemVerilog learning 4 (randomization)
- Paddlepaddle 29 dynamically modify the network structure without model definition code (relu changes to prelu, conv2d changes to conv3d, 2D semantic segmentation model changes to 3D semantic segmentat
- Operation suggestions for today's spot Silver
- 2022焊工(初级)判断题及在线模拟考试
- json 数据展平pd.json_normalize
- Content of string
猜你喜欢
Explore Cassandra's decentralized distributed architecture
2022 tea master (intermediate) examination questions and mock examination
[quickstart to Digital IC Validation] 15. Basic syntax for SystemVerilog Learning 2 (operator, type conversion, loop, Task / Function... Including practical exercises)
[unity] several ideas about circular motion of objects
Qt学习28 主窗口中的工具栏
这5个摸鱼神器太火了!程序员:知道了快删!
【數字IC驗證快速入門】15、SystemVerilog學習之基本語法2(操作符、類型轉換、循環、Task/Function...內含實踐練習)
探索Cassandra的去中心化分布式架构
Record a stroke skin bone error of the skirt
自定义类加载器加载网络Class
随机推荐
Lattice coloring - matrix fast power optimized shape pressure DP
Quickly use Jacobo code coverage statistics
Force buckle 145 Binary Tree Postorder Traversal
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 ?
Value sequence (subsequence contribution problem)
Visualization Document Feb 12 16:42
Pytorch parameter initialization
【数字IC验证快速入门】15、SystemVerilog学习之基本语法2(操作符、类型转换、循环、Task/Function...内含实践练习)
Summary of redis functions
芯片资料 网站 易特创芯
[quick start of Digital IC Verification] 17. Basic grammar of SystemVerilog learning 4 (randomization)
Explore Cassandra's decentralized distributed architecture
Button wizard collection learning - mineral medicine collection and running map
Thinkcmf6.0 installation tutorial
[Stanford Jiwang cs144 project] lab3: tcpsender
Force buckle 144 Preorder traversal of binary tree
快速使用 Jacoco 代码覆盖率统计
Zhilian + AV, AITO asked M7 to do more than ideal one
Introduction to basic components of wechat applet