当前位置:网站首页>Leetcode simple question: find the K beauty value of a number
Leetcode simple question: find the K beauty value of a number
2022-07-07 08:09:00 【·Starry Sea】
subject
An integer num Of k The beauty value is defined as num The following conditions are met in Substring number :
Substring length is k .
Substrings can be divided by num .
Give you an integer num and k , Please return num Of k Beauty is worth .
Be careful :
Allow Prefix 0 .
0 You can't divide any value .
One Substring Is a sequence of consecutive characters in a string .
Example 1:
Input :num = 240, k = 2
Output :2
explain : Here are num The inner length is k Substring of :
- “240” Medium “24” :24 aliquot 240 .
- “240” Medium “40” :40 aliquot 240 .
therefore ,k The beauty value is 2 .
Example 2:
Input :num = 430043, k = 2
Output :2
explain : Here are num The inner length is k Substring of :
- “430043” Medium “43” :43 aliquot 430043 .
- “430043” Medium “30” :30 Not divisible 430043 .
- “430043” Medium “00” :0 Not divisible 430043 .
- “430043” Medium “04” :4 Not divisible 430043 .
- “430043” Medium “43” :43 aliquot 430043 .
therefore ,k The beauty value is 2 .
Tips :
1 <= num <= 10^9
1 <= k <= num.length ( take num As a string )
source : Power button (LeetCode)
Their thinking
The simple way is , Will be given num It is transformed into a string and then traversed to determine whether each substring meets the conditions . Here we need to pay attention to dividing by 0 To deal with .
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
边栏推荐
- Complex network modeling (I)
- Qt学习27 应用程序中的主窗口
- [quick start of Digital IC Verification] 15. Basic syntax of SystemVerilog learning 2 (operators, type conversion, loops, task/function... Including practical exercises)
- 海信电视开启开发者模式
- 【数字IC验证快速入门】15、SystemVerilog学习之基本语法2(操作符、类型转换、循环、Task/Function...内含实践练习)
- Codeforce c.strange test and acwing
- Find the mode in the binary search tree (use medium order traversal as an ordered array)
- Record a stroke skin bone error of the skirt
- JS quick start (I)
- Leetcode 40: combined sum II
猜你喜欢
【数字IC验证快速入门】10、Verilog RTL设计必会的FIFO
Explore dry goods! Apifox construction ideas
These five fishing artifacts are too hot! Programmer: I know, delete it quickly!
Empire CMS collection Empire template program general
【数字IC验证快速入门】12、SystemVerilog TestBench(SVTB)入门
Few shot Learning & meta learning: small sample learning principle and Siamese network structure (I)
The zblog plug-in supports the plug-in pushed by Baidu Sogou 360
The charm of SQL optimization! From 30248s to 0.001s
Force buckle 145 Binary Tree Postorder Traversal
2022 Inner Mongolia latest advanced fire facility operator simulation examination question bank and answers
随机推荐
【无标题】
【数字IC验证快速入门】12、SystemVerilog TestBench(SVTB)入门
ZCMU--1396: 队列问题(2)
JS复制图片到剪切板 读取剪切板
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
uniapp 移动端强制更新功能
Merging binary trees by recursion
The principle and implementation of buffer playback of large video files
Recursive construction of maximum binary tree
Leetcode 90: subset II
这5个摸鱼神器太火了!程序员:知道了快删!
json 数据展平pd.json_normalize
Téléchargement des données de conception des puces
LeetCode中等题之我的日程安排表 I
Binary tree and heap building in C language
Thinkcmf6.0 installation tutorial
WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after conne
复杂网络建模(二)
C语言队列
Complex network modeling (III)