[容易] Self Dividing Numbers

[容易] Self Dividing Numbers

[题]

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.

Input:
left = 1, right = 22
Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15, 22]

[解]

Method1
将每个数字的个位,十位,百位..取出,验证是否 self-dividing

128 % 10 = 8
(128 / 10) % 10 = 2
(128 / 10 / 10) % 10 = 1

Method2 
A straightforward approach to that problem would be to convert the number into a character array (string in Python), and then convert back to integer to perform the modulo operation when checking n % d == 0

We could also continually divide the number by 10 and peek at the last digit. That is shown as a variation in a comment.

 

java.lang.String.valueOf: String — Returns the string representation of the int, float, double… argument.

java.lang.String.toCharArray: char[] — Converts this string to a new character array