Topic:
A self-dividing number is a number which is divisible by every digit it contains.
For example, 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 Possib Le.
Test instructions
Self-dividing number can be understood as a self divisor, and the third line shows that the divisor cannot contain numbers 0
Rules for judging self-divisor:
Separate a number num by bit (using the take-up operation%10 each time the single digit is taken out) to determine whether the number num can be divided by the number of separated, if all the bits can be divisible by the current number of NUM, then this number num is the self divisor
Input/output format:
Input:left = 1, right = 22Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15, 22]
Enter the minimum and maximum values (left and right) to determine all the divisor within this range and return a List
Python:
classsolution (object):defselfdividingnumbers (self, left, right):""": Type Left:int:type right:int:rtype:list[int]"""Rat=[] forIinchRange (left,right+1): Num=Len (str (i)) flag=1J=I while(i): t=i%10if notTorJ%t! =0:flag=0 BreakI/=10ifFlag:rat.append (j)returnRat
Grammar Summary:
Rat=[] can declare an empty list, and if the current number is a self-divisor, use Rat.append () to add the current number to the list
When you use the left and right scopes, you need to right+1
If after must follow a colon
Num=len (str (i)) can find the number of digits of an int type
Algorithm--leetcode 728. Self dividing Numbers