Self Dividing Numbers

• Why not just check if the digit is 0. return false. and in condition put number>0 as usual. No need to convert.

• Solution in golang very straightforward

func selfDivide(left, right int) []int {
if left > right {
return nil
}
result := make([]int, 0)
for i := left; i <= right; i++ {
digits := extractDigits(i)
count := 0
for _, d := range digits {
if d != 0 && i%d == 0 {
count++
}
}
if count == len(digits) {
result = append(result, i)
}
}
return result
}

// extracts digits in reverse order as the order does not matter
func extractDigits(n int) []int {
digits := make([]int, 0)
for n != 0 {
r := n % 10
digits = append(digits, r)
n = n / 10
}
return digits
}

• My attempt in 1-liner:

``````class Solution(object):
def selfDividingNumbers(self, left, right):
return [x for x in range(left,right+1) if not ('0' in set(str(x)) or any (x%(ord(ch)-48) for ch in set(str(x))))]
``````

• I think there is a misconception with the approach of using mod/division to examine digits.

You state:
"One approach that seems promising is to continually divide the number by 10 and peek at the last digit. The problem with that approach is that it is unclear how to differentiate that 10 has the digits '1' and '0' and isn't self-dividing, while 1 has the digits '1' and is self-dividing, as the stopping condition would be number > 0."

However, you can still examine digits of '10' and '1' just fine:

do this:

int tmp = currentNumber;
while (tmp > 0) {
int digit = tmp % 10;
if (digit == 0 || currentNumber % digit != 0) {
// this number is not self dividing
break;
}
tmp = tmp / 10;
}

This implementation will work

• @wierzba @jassiuf93 Thanks for the comments, the article has been corrected.

• Hi,

The part

ans = []
for n in range(left, right + 1):
if self_dividing(n):
ans.append(n)

of the code can be written simply as

ans = [n for n in range(left, right + 1) if self_dividing(n)].

It probably does not make the algorithm faster but just looks a bit more elegant.

• @TheStrayCat Thanks, that is why I had placed a comment there to that effect. However, the article is aimed at people that would have difficulty solving this problem so I also tried to balance that concern.

• The following code seems to be wrong!

``````/* public boolean selfDividing(int n) {
int x = n;
while (x > 0) {
int d = x % 10;
x /= 10;
if (d == 0 || (x % d) > 0) return false;
}
return true;
*/
``````

We should do `if (d == 0 || (n % d) > 0) return false;`. Also a `}` is mssing.

• This post is deleted!

• This post is deleted!

• @jianchao.li.fighter Welcome back! You can edit your own comment by scrolling to the bottom of page and click on "View original thread". Then you can scroll to your own post and click on the three vertical dots icon on the far right side of your post. A menu should pop out and you can click on "Edit post" to edit your comment.

• class Solution {
public:
vector<int> selfDividingNumbers(int left, int right) {

``````    vector<int> final_ans;
for(int i = left; i <= right; i++){
int temp = i;
int count=0, n = 0;
int base = 10;
while(temp){
n++;
temp /= 10;
}

temp = i;
int num = 0;
while(temp){
num = temp%10;
if(num != 0 && i % num == 0){
count++;
}
temp /= 10;
}

if(n == count){
final_ans.push_back(i);
}

}

return final_ans;
}
``````

};

• @1337c0d3r Thank you for the detailed instructions. It is amazing to see so many nice features have been added to LeetCode :-)

• Here's my Kotlin solution:

``````class Solution {
fun selfDividingNumbers(left: Int, right: Int): List<Int> {
var list:MutableList<Int> = mutableListOf<Int>()
for (num in left..right){
}
return list
}

fun isSDN(num: Int) : Boolean {
var num_ = num
while (num_ > 0){
val digit = num_ % 10
if (digit == 0 || num % digit != 0) return false
num_ /= 10
}
return true
}
}
``````

• my C++ solution

``````    vector<int> selfDividingNumbers(int left, int right) {
vector<int> vec;
for(int i = left; i <= right; i++)
{
int val = i, num = i;
int tmp = 0;
while(num)
{
if(num / 10 >= 1 && num != 10) //num > 10
{
tmp = num % 10;
if(tmp == 0)
break;
else
{
if(val % tmp == 0)
{
num /= 10;
continue;
}
else
break;
}
}
else if(num == 10) //num = 10
{
break;
}
else if(num / 10 == 0) //num < 10
{
if(val % num == 0)
vec.push_back(val);
break;
}
}
}
return vec;
}
``````

• I have simplified my code like below

``````class Solution {
public:
vector<int> selfDividingNumbers(int left, int right) {
vector<int> vec;
for(int i = left; i <= right; i++)
{
int val = i, num = i;
while(num)
{
if(num > 10)
{
if(num % 10 == 0) break;
else
{
if(val % (num % 10) == 0) num /= 10;
else break;
}
}
else if(num == 10) break;
else //num < 10
{
if(val % num == 0)
vec.push_back(val);
break;
}
}
}
return vec;
}
};
``````

• Swift Solution

``````func selfDividingNumbers(_ left: Int, _ right: Int) -> [Int] {
var res = [Int]()
for num in left...right {
var found = true
let digits = String(num).flatMap ({ Int(String(\$0)) }) // number to digits
for n in digits { found = found && (n != 0 && num % n == 0) }
if found { res += [num] }
}
return res
}
``````

• Javascript Solution

``````/**
* @param {number} left
* @param {number} right
* @return {number[]}
*/
var selfDividingNumbers = function(left, right) {
var result = []
//从left到right的每一个数n，看是否n每一位都能整除n，如果是，就将n加入result数组
//n每一位都不能是0
for(var i = left; i <= right; i++) {
var tmpI = i, digit = 0, isDividingNum
while(tmpI) {
isDividingNum = 1
digit = tmpI % 10
if(!digit || i % digit != 0) {
isDividingNum = 0
break
}

tmpI = (tmpI - digit) / 10
}

if(isDividingNum) {
result.push(i)
}
}

return result
};
``````

• /**

• @param {number} left
• @param {number} right
• @return {number[]}
*/
var selfDividingNumbers = function(left, right) {
var result=[];
for(var i=left;i<=right;i++){
var allTrue=true;
// console.log(i.toString().length,i.toString()[0]);
if(i.toString().length>1){
for(var j=0;j<i.toString().length;j++){
//console.log(i,i.toString()[j],(i%i.toString()[j]==0))
if(i%i.toString()[j]!=0 || i.toString()[j]==0){
allTrue=false;
break;
}
}
}
if(allTrue)
result.push(i);
}
return result;
};

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.