# [LeetCode] Self Dividing Numbers 自整除数字

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.

public:
vector<int> selfDividingNumbers(int left, int right) {
vector<int> res;
for (int i = left; i <= right; ++i) {
if (check(i)) res.push_back(i);
}
return res;
}
bool check(int num) {
string str = to_string(num);
for (char c : str) {
if (c == '0' || num % (c - '0')) return false;
}
return true;
}
};

public:
vector<int> selfDividingNumbers(int left, int right) {
vector<int> res;
for (int i = left, n = 0; i <= right; ++i) {
for (n = i; n > 0; n /= 10) {
if (n % 10 == 0 || i % (n % 10) != 0) break;
}
if (n == 0) res.push_back(i);
}
return res;
}
};

https://discuss.leetcode.com/topic/111201/java-c-clean-code

，如需转载请自行联系原博主。

|
4月前
[leetcode 数位计算]2520. 统计能整除数字的位数
[leetcode 数位计算]2520. 统计能整除数字的位数
46 0
|
12月前
|

|

LeetCode - #2 Add Two Numbers

70 0
|

LeetCode只出现一次的数字使用JavaScript解题|前端学算法
LeetCode只出现一次的数字使用JavaScript解题|前端学算法
126 0
|

127 0
|

127 0
|

103 0
LeetCode 1380. 矩阵中的幸运数 Lucky Numbers in a Matrix
LeetCode 1380. 矩阵中的幸运数 Lucky Numbers in a Matrix
133 0
LeetCode Contest 178-1365. 有多少小于当前数字的数字 How Many Numbers Are Smaller Than the Current Number
LeetCode Contest 178-1365. 有多少小于当前数字的数字 How Many Numbers Are Smaller Than the Current Number
85 0
|

LeetCode 136. 只出现一次的数字 Single Number
LeetCode 136. 只出现一次的数字 Single Number
61 0

DDNS