内容简介:时间 O(NM)从左到右,对每个数字依次取出其各个位的数字,然后判断是否能整除。
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.
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.
暴力法
复杂度
时间 O(NM)
思路
从左到右,对每个数字依次取出其各个位的数字,然后判断是否能整除。
代码
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
}
以上所述就是小编给大家介绍的《[Leetcode] Self Dividing Numbers 自除数》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
七周七语言(卷2)
【美】Bruce A. Tate(泰特)、Fred Daoud(达乌德)、Ian Dees(迪斯) / 7ML翻译组 / 人民邮电出版社 / 2016-12 / 59
深入研习对未来编程具有重要意义的7种语言 Lua、Factor、Elixir、Elm、Julia、Idris和MiniKanren 本书带领读者认识和学习7种编程语言,旨在帮助读者探索更为强大的编程工具。 本书延续了同系列的畅销书《七周七语言》《七周七数据库》和《七周七Web开发框架》的体例和风格。 全书共8章,前7章介绍了Lua、Factor、Elm、Elixir、Jul......一起来看看 《七周七语言(卷2)》 这本书的介绍吧!