LeetCode Self Dividing Numbers

来源:互联网 发布:雪梨开的淘宝店店名 编辑:程序博客网 时间:2024/05/29 14:27

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.

Note:
The boundaries of each input argument are 1 <= left <= right <= 10000.

自除数:这个数可以整除他包含的每个数字
eg: 128。128除以1,2,8都能整除
自除数要求不能包含0
求解包含左右边界在内的所有自除数

class Solution {    public List<Integer> selfDividingNumbers(int left, int right) {        int temp=0;        int sub=0;        List<Integer> result = new ArrayList<Integer>();        for(int i=left; i<=right; i++){            temp = i/10;            sub = i%10;            while(temp>0){                if(sub !=0 && i%sub == 0){                    sub = temp%10;                    temp = temp/10;                }else{                    break;                }            }            if(temp == 0 && sub != 0 && i%sub == 0){                result.add(i);            }        }        return result;    }}
原创粉丝点击