Count the Digits That Divide a Number - Modulo 10 [JS]
Solution: Modulo 10
Find the last digit of the number: n % 10
.
Use Math.floor(n / 10)
to remove the last digit.
Repeat this until n
becomes 0
.
Time Complexity: O(log(n))
58ms
Space Complexity: O(1)
41.7MB
var countDigits = function(num) {
let n = num, ans = 0;
while (n > 0) {
let digit = n % 10;
if (num % digit === 0) ans++;
n = Math.floor(n / 10);
}
return ans;
};
Comments
Post a Comment