LeetCode 2520: Count the Digits That Divide a Number (Digit Extraction + Divisibility Check)
LeetCode 2520MathDigit ProcessingToday we solve LeetCode 2520 - Count the Digits That Divide a Number.
Source: https://leetcode.com/problems/count-the-digits-that-divide-a-number/
English
Problem Summary
Given an integer num, count how many of its digits can divide num exactly. Digits equal to 0 do not divide any number and must be skipped.
Key Insight
Keep the original number unchanged for modulo checks, and use a copy to extract digits one by one via % 10 and / 10. For each extracted digit d, count it if d != 0 and num % d == 0.
Algorithm
- Set x = num and ans = 0.
- While x > 0: extract d = x % 10.
- If d != 0 and num % d == 0, increment ans.
- Reduce x = x / 10 (integer division).
- Return ans.
Complexity Analysis
Let k be the number of digits in num.
Time: O(k).
Space: O(1).
Common Pitfalls
- Forgetting to skip digit 0 causes division-by-zero errors.
- Accidentally using the changing value x for modulo check instead of original num.
- Misunderstanding repeated digits: each occurrence should be counted independently.
Reference Implementations (Java / Go / C++ / Python / JavaScript)
class Solution {
public int countDigits(int num) {
int x = num;
int ans = 0;
while (x > 0) {
int d = x % 10;
if (d != 0 && num % d == 0) {
ans++;
}
x /= 10;
}
return ans;
}
}func countDigits(num int) int {
x := num
ans := 0
for x > 0 {
d := x % 10
if d != 0 && num%d == 0 {
ans++
}
x /= 10
}
return ans
}class Solution {
public:
int countDigits(int num) {
int x = num;
int ans = 0;
while (x > 0) {
int d = x % 10;
if (d != 0 && num % d == 0) {
++ans;
}
x /= 10;
}
return ans;
}
};class Solution:
def countDigits(self, num: int) -> int:
x = num
ans = 0
while x > 0:
d = x % 10
if d != 0 and num % d == 0:
ans += 1
x //= 10
return ansvar countDigits = function(num) {
let x = num;
let ans = 0;
while (x > 0) {
const d = x % 10;
if (d !== 0 && num % d === 0) {
ans++;
}
x = Math.floor(x / 10);
}
return ans;
};中文
题目概述
给定整数 num,统计它的每一位数字中,有多少个数字可以整除 num。数字 0 不能作为除数,需要跳过。
核心思路
保留原始值 num 用于整除判断,同时用副本 x 逐位拆分数字。每次取出末位 d = x % 10,若 d != 0 且 num % d == 0,计数加一。
算法步骤
- 初始化 x = num、ans = 0。
- 当 x > 0 时,取末位 d = x % 10。
- 若 d != 0 且 num % d == 0,则 ans++。
- 执行 x = x / 10(整数除法)继续处理下一位。
- 循环结束后返回 ans。
复杂度分析
设 num 的位数为 k。
时间复杂度:O(k)。
空间复杂度:O(1)。
常见陷阱
- 忘记跳过 0 会触发除零错误。
- 用不断变化的 x 去做取模判断,导致结果错误(应使用原始 num)。
- 忽略重复数字:相同数字出现多次应分别统计。
多语言参考实现(Java / Go / C++ / Python / JavaScript)
class Solution {
public int countDigits(int num) {
int x = num;
int ans = 0;
while (x > 0) {
int d = x % 10;
if (d != 0 && num % d == 0) {
ans++;
}
x /= 10;
}
return ans;
}
}func countDigits(num int) int {
x := num
ans := 0
for x > 0 {
d := x % 10
if d != 0 && num%d == 0 {
ans++
}
x /= 10
}
return ans
}class Solution {
public:
int countDigits(int num) {
int x = num;
int ans = 0;
while (x > 0) {
int d = x % 10;
if (d != 0 && num % d == 0) {
++ans;
}
x /= 10;
}
return ans;
}
};class Solution:
def countDigits(self, num: int) -> int:
x = num
ans = 0
while x > 0:
d = x % 10
if d != 0 and num % d == 0:
ans += 1
x //= 10
return ansvar countDigits = function(num) {
let x = num;
let ans = 0;
while (x > 0) {
const d = x % 10;
if (d !== 0 && num % d === 0) {
ans++;
}
x = Math.floor(x / 10);
}
return ans;
};
Comments