码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 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
  • 相关阅读:
    NIO流—理解Buffer、Channel概念和NIO的读写操作
    使用 Stable Diffusion Img2Img 生成、放大、模糊和增强
    云服务器的先驱,亚马逊云科技海外云服务器领军者
    【YOLO系列】YOLOv1学习(PyTorch)原理加代码
    多实例tomcat+nginx实现负载均衡
    实验五 定时器
    Go For Web:Golang http 包详解(源码剖析)
    Java网络编程
    typscript中逆变与协变
    QLC SSD适用的应用场景有哪些?附具体案例分享
  • 原文地址:https://blog.csdn.net/wangjun861205/article/details/126556507
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号