Self Dividing Numbers


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
}

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 selfdividing, while 1 has the digits '1' and is selfdividing, 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;
}
result.add(currentNumber);This implementation will work


@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.

@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){ if (isSDN(num)) list.add(num) } 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;
};