当前位置:网站首页>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
边栏推荐
- JS复制图片到剪切板 读取剪切板
- 快解析内网穿透助力外贸管理行业应对多种挑战
- [step on the pit series] H5 cross domain problem of uniapp
- Notes on PHP penetration test topics
- 追风赶月莫停留,平芜尽处是春山
- Minimum absolute difference of binary search tree (use medium order traversal as an ordered array)
- 海信电视开启开发者模式
- Search for an element in a binary search tree (BST)
- 基于Pytorch 框架手动完成线性回归
- JSON data flattening pd json_ normalize
猜你喜欢
Linux server development, MySQL transaction principle analysis
Leetcode 90: subset II
Qt学习26 布局管理综合实例
[step on the pit series] H5 cross domain problem of uniapp
Main window in QT learning 27 application
Shell 脚本的替换功能实现
Hisense TV starts the developer mode
Empire CMS collection Empire template program general
Jmeter 的使用
UnityHub破解&Unity破解
随机推荐
Qt学习28 主窗口中的工具栏
Ansible
电池、电机技术受到很大关注,反而电控技术却很少被提及?
Content of string
QT learning 28 toolbar in the main window
QT learning 26 integrated example of layout management
Thinkcmf6.0 installation tutorial
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
互动送书-《Oracle DBA工作笔记》签名版
Force buckle 145 Binary Tree Postorder Traversal
LeetCode简单题之找到一个数字的 K 美丽值
【踩坑系列】uniapp之h5 跨域的问题
Rust versus go (which is my preferred language?)
Excel import function of jeesite form page
ZCMU--1396: 队列问题(2)
这5个摸鱼神器太火了!程序员:知道了快删!
2022 Inner Mongolia latest advanced fire facility operator simulation examination question bank and answers
Dedecms collects content without writing rules
Merging binary trees by recursion
微信小程序基本组件使用介绍