Self Dividing Numbers
Problem
A self-dividing number is a number that is divisible by every digit it contains.
- For example,
128
is a self-dividing number because128 % 1 == 0
,128 % 2 == 0
, and128 % 8 == 0
.
A self-dividing number is not allowed to contain the digit zero.
Given two integers left
and right
, return a list of all the self-dividing numbers in the range [left, right]
.
Constraints
1 <= left <= right <= 104
Solution
The problem Self Dividing Numbers
can be solved by checking all digits for all integers within the range [left, right]
.
Implementation
static const int fast_io = []()
{
std::ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
return 0;
}();
class Solution
{
public:
vector<int> selfDividingNumbers(int left, int right)
{
vector<int> ret;
do
{
int num = left;
bool check = true;
do
{
int digit = num % 10;
if (digit == 0 || left % digit)
{
check = false;
break;
}
} while ((num /= 10));
if (check)
ret.push_back(left);
} while (++left <= right);
return ret;
}
};