当前位置:网站首页>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
边栏推荐
- C language communication travel card background system
- Chip design data download
- Call pytorch API to complete linear regression
- jeeSite 表单页面的Excel 导入功能
- Recursive method to verify whether a tree is a binary search tree (BST)
- paddlepaddle 29 无模型定义代码下动态修改网络结构(relu变prelu,conv2d变conv3d,2d语义分割模型改为3d语义分割模型)
- 2022年全国最新消防设施操作员(初级消防设施操作员)模拟题及答案
- Custom class loader loads network class
- 快解析内网穿透助力外贸管理行业应对多种挑战
- These five fishing artifacts are too hot! Programmer: I know, delete it quickly!
猜你喜欢
Dedecms collects content without writing rules
互动送书-《Oracle DBA工作笔记》签名版
复杂网络建模(一)
Hisense TV starts the developer mode
Network learning (III) -- highly concurrent socket programming (epoll)
Thinkcmf6.0 installation tutorial
微信小程序基本组件使用介绍
jeeSite 表单页面的Excel 导入功能
UnityHub破解&Unity破解
These five fishing artifacts are too hot! Programmer: I know, delete it quickly!
随机推荐
贝叶斯定律
【数字IC验证快速入门】12、SystemVerilog TestBench(SVTB)入门
Linux server development, detailed explanation of redis related commands and their principles
2022 National latest fire-fighting facility operator (primary fire-fighting facility operator) simulation questions and answers
Visualization Document Feb 12 16:42
LeetCode中等题之我的日程安排表 I
Téléchargement des données de conception des puces
Dedecms collects content without writing rules
ROS bridge notes (05) - Carla_ ackermann_ Control function package (convert Ackermann messages into carlaegovehiclecontrol messages)
Complex network modeling (I)
青龙面板--花花阅读
Linux Installation MySQL 8.0 configuration
电池、电机技术受到很大关注,反而电控技术却很少被提及?
王爽 《汇编语言》之寄存器
追风赶月莫停留,平芜尽处是春山
Chip design data download
Cnopendata American Golden Globe Award winning data
B. Value sequence thinking
Linux server development, MySQL index principle and optimization
【踩坑系列】uniapp之h5 跨域的问题