当前位置:网站首页>LeetCode 5218. 个位数字为 K 的整数之和(枚举)
LeetCode 5218. 个位数字为 K 的整数之和(枚举)
2022-06-22 00:37:00 【Michael阿明】
1. 题目
给你两个整数 num 和 k ,考虑具有以下属性的正整数多重集:
- 每个整数个位数字都是 k 。
- 所有整数之和是 num 。
返回该多重集的最小大小,如果不存在这样的多重集,返回 -1 。
注意:
多重集与集合类似,但多重集可以包含多个同一整数,空多重集的和为 0 。
个位数字 是数字最右边的数位。
示例 1:
输入:num = 58, k = 9
输出:2
解释:
多重集 [9,49] 满足题目条件,和为 58 且每个整数的个位数字是 9 。
另一个满足条件的多重集是 [19,39] 。
可以证明 2 是满足题目条件的多重集的最小长度。
示例 2:
输入:num = 37, k = 2
输出:-1
解释:个位数字为 2 的整数无法相加得到 37 。
示例 3:
输入:num = 0, k = 7
输出:0
解释:空多重集的和为 0 。
提示:
0 <= num <= 3000
0 <= k <= 9
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/sum-of-numbers-with-units-digit-k
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
2. 解题
- 特殊情况先考虑,然后再考虑个位数个数从 1 - 10 个,能否得到 num 的个位数,注意 k*个数 <= num
class Solution:
def minimumNumbers(self, num: int, k: int) -> int:
if num==0:
return 0
if k==0 and num%10==0: # num = 10, k = 0
return 1
for n in range(1, 11):
if (k*n)%10 == num%10 and k*n <= num:
return n
return -1
36 ms 14.8 MB Python3
我的CSDN博客地址 https://michael.blog.csdn.net/
长按或扫码关注我的公众号(Michael阿明),一起加油、一起学习进步!
边栏推荐
- [dailyfresh] course record
- [environment stepping on pit] error occurs when uploading files using fastdfs test
- SparkRDD 案例:计算总成绩
- Evc4 program cannot run on the emulator
- 香橙派orangepi4b上安装tensorflow与transformer
- isnull() ifnull() nullif()
- 【ÑÖÏ模拟赛】花萎(矩阵加速,循环卷积,高斯消元)
- Pytorch learning 09: basic matrix operations
- 4g/wifi energy consumption metering socket - monitoring voltage, current and power
- 動態規劃-01背包,分割等和子集,最後一塊石頭的重量
猜你喜欢

单点登录SSO与OAuth2 方案

03 FastJson 解决循环引用
![3 minutes, take you to play with chat robot automation [top template]](/img/71/4d848b46a52b71a351a086db248a95.png)
3 minutes, take you to play with chat robot automation [top template]

4G/wifi 能耗计量插座-监测电压电流功率

Pytorch learning 13: implement letnet and learning nn Module related basic operations

How to remove duplication in left join from a simple example

Broadening - simple strategy test

Pytorch learning 04: creation of tensor

Compilation principle - recursive descent subroutine method

【ÑÖÏ模拟赛】花萎(矩阵加速,循环卷积,高斯消元)
随机推荐
crf*. Handling of large BDB file
Sparkrdd case: calculate total score
Interview question catalog collection
[environment stepping on pit] error occurs when uploading files using fastdfs test
Judge whether the system CPU is idle
想加入大厂?看这篇文章也许会帮助到你
03 FastJson 解决循环引用
[dailyfresh] course record
从简单实例来看 left join 如何去重
Error 4 opening dom ASM/Self in 0x8283c00
Cancellation meeting and automatic refund processing of message queue
LVGL的一些介绍和移植
Simple sorting of RNN
对“基于tensorflow+RNN的新浪新闻文本分类”一文的图示化理解
Error 4 opening dom ASM/Self in 0x8283c00
3746. academic circle of cattle II
pm2 的学习
clean,compile,build,install,package区别
Pytorch learning 06: tensor dimension transformation
Pytorch learning 05: indexing and slicing