https://leetcode.com/problems/self-dividing-numbers/description/
A self-dividing number is a number that is divisible by every digit it contains.
For example, 128 is a self-dividing number because 128 % 1 == 0, 128 % 2 == 0, and 128 % 8 == 0.
Also, a self-dividing number is not allowed to contain the digit zero.
Given a lower and upper number bound, output a list of every possible self dividing number, including the bounds if possible.
Example 1:
Input:
left = 1, right = 22
Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15, 22]
Note:
The boundaries of each input argument are 1 <= left <= right <= 10000.
bool checkIsSelfDividingNumber(int num)
{
if (num < 10)
return true;
int tmp = num;
while (tmp > 1)
{
int ge = tmp % 10;
if (ge == 0)
{
return false;
}
else if (num % ge != 0)
{
return false;
}
tmp = tmp / 10;
}
return true;
}
// 728. Self Dividing Numbers
// For example, 128 is a self-dividing number because 128 % 1 == 0, 128 % 2 == 0, and 128 % 8 == 0.
// ac
vector<int> solution::selfDividingNumbers(int left, int right)
{
vector<int> res ;
for (int i = left; i <= right; ++i)
{
if (checkIsSelfDividingNumber(i))
{
res.push_back(i);
}
}
return res;
}
本文详细介绍了一种算法,用于找出指定范围内的所有自除数。自除数是指能被其包含的所有数字整除的数,且不包含0。文章通过示例解释了自除数的概念,并提供了一个检查函数checkIsSelfDividingNumber来验证一个数是否为自除数,最后通过solution::selfDividingNumbers函数遍历指定范围内的所有数,返回所有符合条件的自除数列表。

被折叠的 条评论
为什么被折叠?



