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

边栏推荐
- Hands on deep learning (IV) -- convolutional neural network CNN
- Lattice coloring - matrix fast power optimized shape pressure DP
- [2022 ciscn] replay of preliminary web topics
- Qt学习28 主窗口中的工具栏
- Téléchargement des données de conception des puces
- 青龙面板--花花阅读
- LeetCode 40:组合总和 II
- [advanced digital IC Verification] command query method and common command interpretation of VCs tool
- Leetcode 40: combined sum II
- [matlab] when matrix multiplication in Simulink user-defined function does not work properly, matrix multiplication module in module library can be used instead
猜你喜欢

Most elements

Common validation comments

Qt学习26 布局管理综合实例

Content of string

Main window in QT learning 27 application

【數字IC驗證快速入門】15、SystemVerilog學習之基本語法2(操作符、類型轉換、循環、Task/Function...內含實踐練習)
![[matlab] when matrix multiplication in Simulink user-defined function does not work properly, matrix multiplication module in module library can be used instead](/img/e3/cceede6babae3c8a24336c81d98aa7.jpg)
[matlab] when matrix multiplication in Simulink user-defined function does not work properly, matrix multiplication module in module library can be used instead

【数字IC验证快速入门】10、Verilog RTL设计必会的FIFO
![[mathematical notes] radian](/img/43/2af510adb24fe46fc0033d11d60488.jpg)
[mathematical notes] radian

Leetcode 90: subset II
随机推荐
2022年茶艺师(中级)考试试题及模拟考试
芯片资料 网站 易特创芯
Hands on deep learning (IV) -- convolutional neural network CNN
[Stanford Jiwang cs144 project] lab3: tcpsender
Leanote private cloud note building
追风赶月莫停留,平芜尽处是春山
Sign up now | oar hacker marathon phase III, waiting for your challenge
Cnopendata list data of Chinese colleges and Universities
有 Docker 谁还在自己本地安装 Mysql ?
[quickstart to Digital IC Validation] 15. Basic syntax for SystemVerilog Learning 2 (operator, type conversion, loop, Task / Function... Including practical exercises)
[Stanford Jiwang cs144 project] lab4: tcpconnection
[UVM basics] summary of important knowledge points of "UVM practice" (continuous update...)
【数字IC验证快速入门】10、Verilog RTL设计必会的FIFO
pytest+allure+jenkins环境--填坑完毕
C language communication travel card background system
[2022 actf] Web Topic recurrence
Visualization Document Feb 12 16:42
What are the positions of communication equipment manufacturers?
Button wizard script learning - about tmall grabbing red envelopes
Qt学习27 应用程序中的主窗口