[Leetcode] Self Dividing Numbers 自除数

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

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

查看所有标签

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

跨平台桌面应用开发:基于Electron与NW.js

跨平台桌面应用开发:基于Electron与NW.js

【丹】Paul B. Jensen / Goddy Zhao / 2018-3 / 99

《跨平台桌面应用开发:基于Electron与NW.js》是一本同时介绍 Electron和 NW.js的图书,这两者是目前流行的支持使用 HTML、CSS 和 JavaScript 进行桌面应用开发的框架。书中包含大量的编码示例,而且每个示例都是五脏俱全的实用应用,作者对示例中的关键代码都做了非常详细的解释和说明,可让读者通过实际的编码体会使用这两款框架开发桌面应用的切实感受。除此之外,在内容上,......一起来看看 《跨平台桌面应用开发:基于Electron与NW.js》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具