博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Swift]LeetCode728. 自除数 | Self Dividing Numbers
阅读量:4885 次
发布时间:2019-06-11

本文共 6074 字,大约阅读时间需要 20 分钟。

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝()
➤GitHub地址:
➤原文地址:  
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

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 == 0128 % 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 = 22Output: [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 == 0128 % 2 == 0128 % 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

Runtime: 8 ms
Memory Usage: 18.8 MB
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) != 011                 {12                     break13                 }14                 n /= 1015             }16             if n == 017             {18                 res.append(i)19             }20         }21         return res22     }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 loop12                 }13                 number = number / 1014             }15             result.append(i)16         }17         return result18     }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 = true12             }13             else { resultTrue = false; break }14             temp = temp / 1015         }16         if resultTrue {17             result.append(num)18         }19     }20     return result21     }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 resultArr12 }13 14 func check(_ num: Int) -> Bool {15     var n = num16     while n != 0 {17         let a = n % 1018         if a == 0 { return false }19         if num % a != 0 { return false }20         n /= 1021     }22     23     return true24   }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 selfDividingVals24     }25 26     func canDivide(with num: Int, zeroCount: Int) -> Bool {27         for i in 0..

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) in15         if num % digit != 0 { result = false }16     }    17     return result ?? true18 }19 20 func digitalize(num: Int) -> [Int] {21     var _num: Int = num22     var result: [Int] = []    23     while _num > 0 {24         if _num % 10 == 0 { return [] }25         result.append(_num % 10)26         _num = _num / 1027     }    28     return result29   }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 result15     }16 }

 

转载于:https://www.cnblogs.com/strengthen/p/10516387.html

你可能感兴趣的文章
Using Flash Builder with Flash Professional
查看>>
jsp/post中文乱码问题
查看>>
C# 插入或删除word分页符
查看>>
数据库数据的查询----连接查询
查看>>
找不到可安装的ISAM ,asp.net读取数据丢失,解决的一列里有字符与数字的
查看>>
Java学习笔记三(对象的基本思想一)
查看>>
Java程序(文件操作)
查看>>
KMP算法 最小循环节 最大重复次数
查看>>
Proving Equivalences (强连通,缩点)
查看>>
Period (KMP算法 最小循环节 最大重复次数)
查看>>
sgu 103. Traffic Lights
查看>>
poj 3621 Sightseeing Cows
查看>>
hdu 3666 THE MATRIX PROBLEM
查看>>
TopCoder SRM 176 Deranged
查看>>
Javascript中数组与字典(即map)的使用
查看>>
memcached(十三)注意事项
查看>>
ITerms2在mac系统下的安装和配色,并和go2shell关联
查看>>
nginx常见面试题1
查看>>
Sublime Text 报“Pylinter could not automatically determined the path to lint.py
查看>>
自动化测试用例getText()获取某一个元素的值返回null或空
查看>>