LeetCode每日一题——2520. Count the Digits That Divide a Number

编程入门 行业动态 更新时间:2024-10-28 13:15:49

LeetCode每日一题——2520. <a href=https://www.elefans.com/category/jswz/34/1759757.html style=Count the Digits That Divide a Number"/>

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.

二、题解

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;}
};

更多推荐

LeetCode每日一题——2520. Count the Digits That Divide a Number

本文发布于:2023-12-03 08:50:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1653408.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Count   LeetCode   Number   Divide   Digits

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!