LeetCode每日一题——2520. Count the Digits That Divide a Number

文章目录

一、题目

2520. Count the Digits That Divide a Number

Given an integer num, return the number of digits in num that divide num.

An integer val divides nums if nums % val == 0.

Example 1:

Input: num = 7

Output: 1

Explanation: 7 divides itself, hence the answer is 1.

Example 2:

Input: num = 121

Output: 2

Explanation: 121 is divisible by 1, but not 2. Since 1 occurs twice as a digit, we return 2.

Example 3:

Input: num = 1248

Output: 4

Explanation: 1248 is divisible by all of its digits, hence the answer is 4.

Constraints:

1 <= num <= 109

num does not contain 0 as one of its digits.

二、题解

cpp 复制代码
class Solution {
public:
    int countDigits(int num) {
        int tmp = num;
        int res = 0;
        while(tmp){
            int mod = tmp % 10;
            if(num % mod == 0) res++;
            tmp = tmp / 10;
        }
        return res;
    }
};
相关推荐
Sam_Deep_Thinking1 小时前
学数据结构到底有什么用
数据结构
kobesdu1 小时前
人形机器人SLAM:技术挑战、算法综述与开源方案
算法·机器人·人形机器人
yolo_guo2 小时前
redis++使用: hmset 与 hmget
c++·redis
handler012 小时前
拒绝权限报错!三分钟掌握 Linux 权限管理
linux·c语言·c++·笔记·学习
椰羊~王小美3 小时前
随机数概念及算法
算法
阿Y加油吧3 小时前
算法实战笔记:LeetCode 169 多数元素 & 75 颜色分类
笔记·算法·leetcode
t***5443 小时前
如何在Dev-C++中选择Clang编译器
开发语言·c++
不要秃头的小孩3 小时前
力扣刷题——509. 斐波那契数
python·算法·leetcode·动态规划
汉克老师4 小时前
GESP2023年9月认证C++三级( 第一部分选择题(9-15))
c++·gesp三级·gesp3级