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;
    }
};
相关推荐
Eloudy1 分钟前
预词力:LLM 的唯一形式化能力
人工智能·算法·agent
码匠许师傅9 分钟前
【C++ 面试真题】聊聊 C++ 中的 lambda 表达式
java·c++·面试
依然鸣18 分钟前
PTA团体程序设计天梯赛L1真题讲解L1-085-088
数据结构·c++·经验分享·算法·深度优先·pat考试
沙盘客43 分钟前
AFSIM 14篇 C++ 插件开发:扩展你的仿真能力
c++·后端
NeilYuen44 分钟前
【vemory】高性能KV存储AOF方案设计
linux·c++·redis·缓存
听取WA声一片(无恶意)1 小时前
CSP-J/S 初赛图论完全讲义
算法·csp-s初赛·csp-j初赛
Brilliantwxx1 小时前
【算法从零到千】【51-54】逆序对(分治+递归算法)
数据结构·算法·排序算法
小星星闪亮登场1 小时前
2026河南萌新联赛第四场--南阳理工学院
数据结构·算法·贪心算法·动态规划·哈希算法·广度优先
土司大王1 小时前
LeetCode hot100——两数之和
数据结构·算法·leetcode
luj_17682 小时前
大航海时代:沉浸式财商实战沙盒
c语言·开发语言·网络·经验分享·算法