Skip to content
Rain Hu's Workspace
Go back

[LeetCode] 2520. Count the Digits That Divide a Number

Rain Hu

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:

Example 2:

Example 3:

Constraints:


二、分析

while (num > 0){
    int digit = num % 10;
    num /= 10;
}
+ 利用上面的方法,再針對題目做相應的統計即可。

三、解題

1. Math

int countDigits(int num) {
    int res = 0;
    int x = num;
    while (x > 0) {
        int tmp = x % 10;
        if (num % tmp == 0) res++;
        x /= 10;
    }
    return res;
}

回目錄 Catalog


Share this post on:

Previous
[LeetCode] 2521. Distinct Prime Factors of Product of Array
Next
[LeetCode] 290. Word Pattern