• 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
  • 相关阅读:
    ExecuteScalar requires the command to have a transaction when the connection assigned to the command is in a pending
    如何从vss中分离程序
    String or binary data would be truncated
    the pop3 service failed to retrieve authentication type and cannot continue
    The POP3 service failed to start because
    IIS Error he system cannot find the file specified _找不到页面
    pku2575Jolly Jumpers
    pku2940Wine Trading in Gergovia
    pku3219二项式系数
    pku1029false coin
  • 原文地址:https://www.cnblogs.com/immiao0319/p/8983763.html
Copyright © 2020-2023  润新知