Leetcode 2520. Count the Digits That Divide a Number
You are given a positive integer num. Return the number of digits in num that divide num.
A digit divides num if num % digit == 0.
num have zeroes as digits?
num?
num guaranteed to be positive?
num as a positive integer.num.Here is the solution in Java:
public class CountDividingDigits {
public int countDigits(int num) {
int count = 0;
int originalNum = num;
while (num > 0) {
int digit = num % 10;
if (digit != 0 && originalNum % digit == 0) {
count++;
}
num /= 10;
}
return count;
}
public static void main(String[] args) {
CountDividingDigits solution = new CountDividingDigits();
int num = 121; // Example number
int result = solution.countDigits(num);
System.out.println("Number of digits in " + num + " that divide it is: " + result);
}
}
num % 10) to get the last digit.num /= 10) to remove the last digit.This approach ensures efficient and clear handling of the problem requirements.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?