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;
    }
};
相关推荐
史蒂芬_丁几秒前
C++深度拷贝例子
java·开发语言·c++
中科院提名者11 分钟前
BPE 算法的硬核拆解——理解词表(Vocabulary)是如何从零训练出来的,以及字符串是如何被切碎的
算法
「QT(C++)开发工程师」41 分钟前
C++11三大核心特性深度解析:类型特征、时间库与原子操作
java·c++·算法
乐分启航1 小时前
SliMamba:十余K参数量刷新SOTA!高光谱分类的“降维打击“来了
java·人工智能·深度学习·算法·机器学习·分类·数据挖掘
你真是饿了2 小时前
算法专题二:滑动窗口
算法
Jordannnnnnnn2 小时前
追赶33名
c++
ccLianLian2 小时前
数论·约数
数据结构·算法
会编程的土豆2 小时前
【数据结构与算法】最短路径---Dijkstra 算法
数据结构·c++·算法
2401_879693872 小时前
C++中的观察者模式实战
开发语言·c++·算法
炽烈小老头2 小时前
【 每天学习一点算法 2026/03/24】寻找峰值
学习·算法