For each digit in the number, check if it divides the number. Since the number does not contain 0 as a digit, we can safely check divisibility for each digit.
classSolution {
public:int countDigits(int num) {
int ans =0, n = num;
while (n >0) {
int d = n %10;
if (num % d ==0) ans++;
n /=10;
}
return ans;
}
};
1
2
3
4
5
6
7
8
9
10
11
12
typeSolutionstruct{}
func (Solution) CountDigits(numint) int {
ans, n:=0, numforn > 0 {
d:=n%10ifnum%d==0 {
ans++ }
n/=10 }
returnans}
1
2
3
4
5
6
7
8
9
10
11
classSolution {
publicintcountDigits(int num) {
int ans = 0, n = num;
while (n > 0) {
int d = n % 10;
if (num % d == 0) ans++;
n /= 10;
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
classSolution {
funcountDigits(num: Int): Int {
var ans = 0var n = num
while (n > 0) {
val d = n % 10if (num % d ==0) ans++ n /=10 }
return ans
}
}
1
2
3
4
5
6
7
8
9
10
classSolution:
defcountDigits(self, num: int) -> int:
ans =0 n = num
while n >0:
d = n %10if num % d ==0:
ans +=1 n //=10return ans
1
2
3
4
5
6
7
8
9
10
11
12
13
14
impl Solution {
pubfncount_digits(num: i32) -> i32 {
letmut ans =0;
letmut n = num;
while n >0 {
let d = n %10;
if num % d ==0 {
ans +=1;
}
n /=10;
}
ans
}
}
1
2
3
4
5
6
7
8
9
10
11
classSolution {
countDigits(num: number):number {
letans=0, n=num;
while (n>0) {
constd=n%10;
if (num%d===0) ans++;
n= Math.floor(n/10);
}
returnans;
}
}