[Leetcode] Self Dividing Numbers 自除数

栏目: 编程工具 · 发布时间: 6年前

内容简介:时间 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 自除数》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Pattern Recognition and Machine Learning

Pattern Recognition and Machine Learning

Christopher Bishop / Springer / 2007-10-1 / USD 94.95

The dramatic growth in practical applications for machine learning over the last ten years has been accompanied by many important developments in the underlying algorithms and techniques. For example,......一起来看看 《Pattern Recognition and Machine Learning》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具