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

边栏推荐
- The zblog plug-in supports the plug-in pushed by Baidu Sogou 360
- Blob 對象介紹
- Quick analysis of Intranet penetration helps the foreign trade management industry cope with a variety of challenges
- Custom class loader loads network class
- Linux server development, MySQL index principle and optimization
- Zsh shell adds automatic completion and syntax highlighting
- 面试题(CAS)
- Codeforce c.strange test and acwing
- 太真实了,原来自己一直没有富裕起来是有原因的
- Network learning (I) -- basic model learning
猜你喜欢

2022 simulated examination question bank and online simulated examination of tea master (primary) examination questions

2022 National latest fire-fighting facility operator (primary fire-fighting facility operator) simulation questions and answers

Linux server development, SQL statements, indexes, views, stored procedures, triggers

Open source ecosystem | create a vibrant open source community and jointly build a new open source ecosystem!

探索干货篇!Apifox 建设思路

Network learning (II) -- Introduction to socket

Thinkcmf6.0 installation tutorial

Myabtis_Plus

快解析内网穿透为文档加密行业保驾护航

Visualization Document Feb 12 16:42
随机推荐
【踩坑系列】uniapp之h5 跨域的问题
快解析内网穿透助力外贸管理行业应对多种挑战
Blob 對象介紹
追风赶月莫停留,平芜尽处是春山
Qt学习27 应用程序中的主窗口
Cnopendata geographical distribution data of religious places in China
Empire CMS collection Empire template program general
ROS bridge notes (05) - Carla_ ackermann_ Control function package (convert Ackermann messages into carlaegovehiclecontrol messages)
JS quick start (I)
ZCMU--1492: Problem D(C语言)
LeetCode中等题之我的日程安排表 I
LeetCode简单题之字符串中最大的 3 位相同数字
Network learning (III) -- highly concurrent socket programming (epoll)
C language communication travel card background system
The charm of SQL optimization! From 30248s to 0.001s
力扣(LeetCode)187. 重复的DNA序列(2022.07.06)
2022 simulated examination question bank and online simulated examination of tea master (primary) examination questions
Lattice coloring - matrix fast power optimized shape pressure DP
Call pytorch API to complete linear regression
海信电视开启开发者模式