LeetCode.728 Self Dividing Numbers

来源:互联网 发布:2016淘宝刷钻价格表 编辑:程序博客网 时间:2024/05/07 21:02

题目:

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 = 22Output: [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.


    分析:

    class Solution {    public List<Integer> selfDividingNumbers(int left, int right) {        //给定边界值包括边界值,返回其中所有元素是否满足自除数(即其对其的每一位都能整除)        List<Integer> list =new ArrayList<Integer>();        for(int i=left;i<=right;i++){            if(isSelfDivid(i)){                list.add(i);            }        }        return list;    }    //判断该数是否满足自除数(该数各个位不包含任何0)    public boolean isSelfDivid(int num){        int temp=num;                while(temp>0){            //任何位数位0或者判断该除数不为0,即返回false            if(temp%10==0||num%(temp%10)>0) return false;            temp/=10;        }        return true;    }}