文章目录
  1. 1. 文档更新说明
  2. 2. 前言
  3. 3. 728. Self Dividing Numbers
    1. 3.1. Example1
    2. 3.2. Note
    3. 3.3. Solution

文档更新说明

  • 最后更新 2018年03月09日
  • 首次更新 2018年03月09日

前言

  使用Swift语言完成LetCode题目,简单难度

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

Example1

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

Note

  The boundaries of each input argument are 1 <= left <= right <= 10000.

Solution

  求自整除数. 也就是这个整数里面包含的每一个数字都可以作为整数的因子(约数)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import Foundation

class Solution {
func selfDividingNumbers(_ left: Int, _ right: Int) -> [Int] {
//先把数组列出来
let arr = [Int](left...right)
var sdArr = [Int]()
for n in arr {
var m = n
while m > 0 {
let r = m % 10
// 被除数不能包含0
guard r != 0 && n % r == 0 else {
break
}
m = m / 10
}
if m == 0 {
sdArr.append(n)
}
}

return sdArr
}
}

let arr = Solution().selfDividingNumbers(1, 50)
print(arr)

文章目录
  1. 1. 文档更新说明
  2. 2. 前言
  3. 3. 728. Self Dividing Numbers
    1. 3.1. Example1
    2. 3.2. Note
    3. 3.3. Solution