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;
    }
};
相关推荐
yy_xzz8 分钟前
【Linux开发】I/O 复用:select 模型
linux·c++·select
小肝一下14 分钟前
每日两道力扣,day6
数据结构·c++·算法·leetcode·双指针·hot100
ByteCraze15 分钟前
大四双非春招学习记录-K 个一组反转链表
数据结构·学习·链表
奶人五毛拉人一块20 分钟前
模板与vector的学习
数据结构·学习·迭代器·vector·模板
ambition2024221 分钟前
【算法详解】飞机降落问题:DFS剪枝解决调度问题
c语言·数据结构·c++·算法·深度优先·图搜索算法
I Promise3423 分钟前
C++ 基础数据结构与 STL 容器详解
开发语言·数据结构·c++
徒 花28 分钟前
Python知识学习08
java·python·算法
chushiyunen28 分钟前
milvus笔记、常用表结构
笔记·算法·milvus
liliangcsdn39 分钟前
ChromaDB距离计算公式示例
人工智能·算法·机器学习