[Swift]LeetCode728. 自除数 | Self Dividing Numbers
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: https://www.cnblogs.com/strengthen/p/10516387.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
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.
Example 1:
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
.
自除数 是指可以被它包含的每一位数除尽的数。
例如,128 是一个自除数,因为 128 % 1 == 0
,128 % 2 == 0
,128 % 8 == 0
。
还有,自除数不允许包含 0 。
给定上边界和下边界数字,输出一个列表,列表的元素是边界(含边界)内所有的自除数。
示例 1:
输入: 上边界left = 1, 下边界right = 22 输出: [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15, 22]
注意:
- 每个输入参数的边界满足
1 <= left <= right <= 10000
。
1 class Solution { 2 func selfDividingNumbers(_ left: Int, _ right: Int) -> [Int] { 3 var res:[Int] = [Int]() 4 var n:Int = 0 5 for i in left...right 6 { 7 n = i 8 while(n > 0) 9 { 10 if n % 10 == 0 || i % (n % 10) != 0 11 { 12 break 13 } 14 n /= 10 15 } 16 if n == 0 17 { 18 res.append(i) 19 } 20 } 21 return res 22 } 23 }
8ms
1 class Solution { 2 func selfDividingNumbers(_ left: Int, _ right: Int) -> [Int] { 3 var result = [Int]() 4 loop: for i in left...right { 5 var number = i 6 while number != 0 { 7 if number % 10 == 0 { 8 continue loop 9 } 10 if i % ( number % 10 ) != 0 { 11 continue loop 12 } 13 number = number / 10 14 } 15 result.append(i) 16 } 17 return result 18 } 19 }
12ms
1 class Solution { 2 func selfDividingNumbers(_ left: Int, _ right: Int) -> [Int] { 3 var result: [Int] = [] 4 var resultTrue = false 5 if left == 1 { result.append(left) } 6 First: for num in left...right { 7 var temp: Int = num 8 while temp > 1 { 9 guard temp % 10 != 0 else { resultTrue = false; break } 10 if num % (temp % 10) == 0 { 11 resultTrue = true 12 } 13 else { resultTrue = false; break } 14 temp = temp / 10 15 } 16 if resultTrue { 17 result.append(num) 18 } 19 } 20 return result 21 } 22 }
12ms
1 class Solution { 2 func selfDividingNumbers(_ left: Int, _ right: Int) -> [Int] { 3 var resultArr: [Int] = [] 4 5 for num in left...right { 6 if check(num) { 7 resultArr.append(num) 8 } 9 } 10 11 return resultArr 12 } 13 14 func check(_ num: Int) -> Bool { 15 var n = num 16 while n != 0 { 17 let a = n % 10 18 if a == 0 { return false } 19 if num % a != 0 { return false } 20 n /= 10 21 } 22 23 return true 24 } 25 }
24ms
1 class Solution { 2 func selfDividingNumbers(_ left: Int, _ right: Int) -> [Int] { 3 return(left...right).filter{ 4 var i = $0 5 while(i > 0){ 6 if (i % 10 == 0) || ($0 % (i % 10) != 0) { break } 7 i /= 10 8 } 9 return i == 0 ? true : false 10 } 11 } 12 }
28ms
1 class Solution { 2 func selfDividingNumbers(_ left: Int, _ right: Int) -> [Int] { 3 let nums = [Int](left...right) 4 var selfDividingVals = [Int]() 5 6 for num in nums { 7 if num < 10 { 8 selfDividingVals.append(num) 9 } else if num <= 100 { 10 if canDivide(with: num, zeroCount: 2) { 11 selfDividingVals.append(num) 12 } 13 } else if num <= 1000 { 14 if canDivide(with: num, zeroCount: 3) { 15 selfDividingVals.append(num) 16 } 17 } else if num <= 10000 { 18 if canDivide(with: num, zeroCount: 4) { 19 selfDividingVals.append(num) 20 } 21 } 22 } 23 return selfDividingVals 24 } 25 26 func canDivide(with num: Int, zeroCount: Int) -> Bool { 27 for i in 0..<zeroCount { 28 // Shift right 29 let exp = Int(pow(Double(10), Double(i))) 30 31 // Get last value 32 let val = (num / exp) % 10 33 34 // See if division works! 35 if val == 0 || num % val != 0 { 36 return false 37 } 38 } 39 return true 40 } 41 }
40ms
1 class Solution { 2 func selfDividingNumbers(_ left: Int, _ right: Int) -> [Int] { 3 var result: [Int] = [] 4 for i in left...right { 5 if i < 10 { result.append(i) } 6 else if isSelfDividing(num: i) { result.append(i) } 7 } 8 return result 9 } 10 11 func isSelfDividing(num: Int) -> Bool { 12 guard let digits: [Int] = digitalize(num: num), digits.count != 0 else { return false } 13 var result: Bool? 14 digits.forEach { (digit) in 15 if num % digit != 0 { result = false } 16 } 17 return result ?? true 18 } 19 20 func digitalize(num: Int) -> [Int] { 21 var _num: Int = num 22 var result: [Int] = [] 23 while _num > 0 { 24 if _num % 10 == 0 { return [] } 25 result.append(_num % 10) 26 _num = _num / 10 27 } 28 return result 29 } 30 }
116ms
1 class Solution { 2 func selfDividingNumbers(_ left: Int, _ right: Int) -> [Int] { 3 var result = [Int]() 4 for x in left...right { 5 if x < 10 { 6 result.append(x) 7 } else { 8 let digits = Array(String(x)).map { Int(String($0))! } 9 if digits.allSatisfy({ $0 != 0 && x % $0 == 0 }) { 10 result.append(x) 11 } 12 } 13 } 14 return result 15 } 16 }