728. 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]
大致意思如下
一个数如果能被其各位数整除,则称其为自除数,先要求找出一个范围能的自除数
思路很简单,除以所有自己的各位数即可
public class selfDividingNumbers { public static List<Integer> selfDividingNumbers(int left, int right) { List<Integer> result = new ArrayList<Integer>(); for(int i=left;i<=right;i++) { if(!judgeZero(i)) { int judge=0; for(int k=0;k<intSize(i).size();k++) { if(i%(intSize(i).get(k))!=0) { judge=1; break; } } if(judge==0) { result.add(i); } } } return result; } //判断数字是否含有0 public static Boolean judgeZero(int judge) { String temp = String.valueOf(judge); if(temp.contains("0")) { return true; } else { return false; } } //获取数字每一位 public static List<Integer> intSize(int judge) { String temp = String.valueOf(judge); List<Integer> a = new ArrayList<Integer>(); for(int i=0;i<temp.length();i++) { char t = temp.charAt(i); a.add(Character.getNumericValue(t)); } return a; } public static void main(String args[]) { System.out.println(selfDividingNumbers(100,200).toString()); } }