728. Self Dividing Numbers可以自己除以自己的数字

[抄题]:

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 = 22
Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15, 22]

 [暴力解法]:

时间分析:

空间分析:

 [优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

不知道怎么取出数字中的每一位数:mod%取,然后每次除10就可以了

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 有数字不变的要求:原来的数要固定住,才能自己除以自己

[二刷]:

[三刷]:

[四刷]:

[五刷]:

  [五分钟肉眼debug的结果]:

[总结]:

不知道怎么取出数字中的每一位数:mod%取余,然后每次除10就可以了

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

取余、除10,很方便

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

 [代码风格] :

class Solution {
    public List<Integer> selfDividingNumbers(int left, int right) {
        List<Integer> res = new LinkedList<Integer>();
        
        for (int i = left; i <= right; i++) {
            if (isSelfDividingNumbers(i)) res.add(i);
        }
        
        return res;
    }
    
    public boolean isSelfDividingNumbers(int n) {
        int original = n;
        while (n != 0) {
            int res = n % 10;
            if (n % 10 == 0) return false;
            if (original % res != 0) return false;
            n /= 10;
        }
        
        return true;
    }
}
View Code

 

posted @ 2018-05-03 09:17  苗妙苗  阅读(314)  评论(0)    收藏  举报