• LeetCode每日一题(2310. Sum of Numbers With Units Digit K)


    Given two integers num and k, consider a set of positive integers with the following properties:

    The units digit of each integer is k.
    The sum of the integers is num.
    Return the minimum possible size of such a set, or -1 if no such set exists.

    Note:

    The set can contain multiple instances of the same integer, and the sum of an empty set is considered 0.
    The units digit of a number is the rightmost digit of the number.

    Example 1:

    Input: num = 58, k = 9
    Output: 2

    Explanation:
    One valid set is [9,49], as the sum is 58 and each integer has a units digit of 9.
    Another valid set is [19,39].
    It can be shown that 2 is the minimum possible size of a valid set.

    Example 2:

    Input: num = 37, k = 2
    Output: -1

    Explanation: It is not possible to obtain a sum of 37 using only integers that have a units digit of 2.

    Example 3:

    Input: num = 0, k = 7
    Output: 0

    Explanation: The sum of an empty set is considered 0.

    Constraints:

    • 0 <= num <= 3000
    • 0 <= k <= 9

    个位数字只能是0-9, 我们用k分别乘以1-10来看有没有能跟num的个位数字吻合的,之所以是1到10, 是因为系数为0的情况只有num为0时才成立,我们可以提前做检查, 而实际num个位数字为0的情况(排除掉num为0的情况), 我们需要乘以10来做检查。如果i * k % 10 == num % 10, 那我们只需要检查i * k是不是小于等于num就可以了。


    impl Solution {
        pub fn minimum_numbers(num: i32, k: i32) -> i32 {
            if num == 0 {
                return 0;
            }
            let units_digit = num % 10;
            for i in 1..=10 {
                if k * i % 10 == units_digit {
                    if num - k * i >= 0 {
                        return i;
                    }
                }
            }
            -1
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
  • 相关阅读:
    K8s: 在Pod中使用亲和性调度节点
    浏览器工作原理
    Docker部署jenkins
    计算机软件著作权评估
    【Flutter】Android Studio的Flutter环境配置
    python实验16_网络爬虫
    计算机毕设(附源码)JAVA-SSM基于JAVA语言的宠物寄养管理
    阿里EasyExcel动态头模板下载,以及下拉框设置
    【开发经验】客户端互踢实现思路
    从小白到架构师(4): Feed 流系统实战
  • 原文地址:https://blog.csdn.net/wangjun861205/article/details/126556507