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;
    }
};
相关推荐
陆嵩3 分钟前
CG 方法(共轭梯度)的数学推导及其算法
算法·cg·共轭梯度·lanczos·arnoldi·正交化·gram-schmidt
twilight_4694 分钟前
机器学习与模式识别——SVM
算法·机器学习·支持向量机
小糯米60111 分钟前
C++ 树
数据结构·c++·算法
liliangcsdn25 分钟前
IMPALA强化学习算法的学习和解读
学习·算法
再难也得平29 分钟前
[LeetCode刷题]283.移动零(通俗易懂的java题解)
java·算法·leetcode
不想看见40430 分钟前
House Robber 基本动态规划:一维--力扣101算法题解笔记
笔记·算法·leetcode·代理模式
掘根32 分钟前
【C++STL】红黑树(RBTree)
数据结构·c++·算法
我笑了OvO32 分钟前
常见位运算及其经典算法题(1)
c++·算法·算法竞赛
Zevalin爱灰灰33 分钟前
方法论——如何设计控制策略架构
算法·架构·嵌入式
wostcdk34 分钟前
基础算法学习1
算法