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;
    }
};
相关推荐
王老师青少年编程8 小时前
csp信奥赛C++高频考点专项训练之前缀和&差分 --【一维前缀和】:求区间和
c++·前缀和·csp·高频考点·信奥赛·求和区间和
黎阳之光8 小时前
黎阳之光:以视频孪生重构智能监盘,为燃机打造新一代智慧电厂大脑
大数据·人工智能·算法·安全·数字孪生
绝知此事9 小时前
【算法突围 02】树形结构与数据库索引:树形结构与数据库索引:从 BST 到 B+ 树的演化与 MySQL 优化
数据库·mysql·算法·面试·b+树
kyle~9 小时前
机器人时间链路---工程流程示例
c++·3d·机器人·ros2
清木!9 小时前
排序算法比较
数据结构·算法·排序算法
吴可可12310 小时前
用Teigha修改并保存CAD文件
数据库·算法·c#
汉克老师10 小时前
GESP6级C++考试语法知识(十七、数据结构(三、认识队列 Queue))
数据结构·c++·队列·gesp6级·gesp六级·数组模拟队列
灰灰勇闯IT11 小时前
ops-reduce:ReduceMax 与 ReduceMean 的并行优化
算法
水木流年追梦11 小时前
大模型入门-Reward 奖励模型训练
开发语言·python·算法·leetcode·正则表达式
JavaWeb学起来11 小时前
Python学习教程(六)数据结构List(列表)
数据结构·python·python基础·python教程