# LWC 59：728. Self Dividing Numbers

## LWC 59：728. Self Dividing Numbers

Problem:

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]

Note:

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

```    public List<Integer> selfDividingNumbers(int left, int right) {
List<Integer> ans = new ArrayList<>();
for (int i = left; i <= right; ++i) {
if (valid(i)) {
}
}
return ans;
}

boolean valid(int num) {
String value = Integer.toString(num);
for (char c : value.toCharArray()) {
if (c == '0') return false;
else {
if (num % (c - '0') != 0) return false;
}
}
return true;
}```

```    public List<Integer> selfDividingNumbers(int left, int right) {
List<Integer> ans = new ArrayList<>();
for (int i = left; i <= right; ++i) {
if (valid(i)) {
}
}
return ans;
}

boolean valid (int num) {
int n = num;
while (num != 0) {
int c = num % 10;
if (c == 0) return false;
else {
if (n % c != 0) return false;
}
num /= 10;
}
return true;
}```

0 条评论

## 相关文章

### 1081: [SCOI2005]超级格雷码

1081: [SCOI2005]超级格雷码 Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 301  Solv...

2576

1193

1761

4320

### cow容器之CopyOnWriteArrayList

java并发容器中有个不得不提的概念，cow（CopyOnWrite） 在jdk1.5之前需要在多线程使用同步集合，一般各位会采用如下方法添加synchroni...

1925

2939

### Leetcode ZigZag Conversion

The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of ...

812

### Java 基础知识点（必知必会其二）

1.如何将数字输出为每三位逗号分隔的格式，例如“1,234,467”？    1 package com.Gxjun.problem; 2 3 i...

3745