[Leetcode] Self Dividing Numbers 自除数

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

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

查看所有标签

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

疯狂Java讲义

疯狂Java讲义

李刚 / 电子工业出版社 / 2012-1-1 / 109.00元

《疯狂Java讲义(附光盘第2版)》是《疯狂Java讲义》的第2版,第2版保持了第1版系统、全面、讲解浅显、细致的特性,全面介绍了新增的Java 7的新特性。 《疯狂Java讲义(附光盘第2版)》深入介绍了Java编程的相关方面,全书内容覆盖了Java的基本语法结构、Java的面向对象特征、Java集合框架体系、Java泛型、异常处理、Java GUI编程、JDBC数据库编程、Java注释、......一起来看看 《疯狂Java讲义》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具