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.git
Also, a self-dividing number is not allowed to contain the digit zero.app
Given a lower and upper number bound, output a list of every possible self dividing number, including the bounds if possible.code
Example 1: Input: left = 1, right = 22 leetcode
Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15, 22]rem
Note:
The boundaries of each input argument are 1 <= left <= right <= 10000.get
時間 O(NM)input
從左到右,對每一個數字依次取出其各個位的數字,而後判斷是否能整除。it
func selfDividingNumbers(left int, right int) []int { var res []int for ; left <= right; left++ { curr := left for curr > 0 { rem := curr % 10 // the digit can't be zero and should be divisible if rem == 0 || left%rem != 0 { break } curr = curr / 10 } if curr == 0 { res = append(res, left) } } return res }