LeetCode——728. Self-Divisor

Title description:

The self-divisor refers to the number that can be divided 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, the self-divisor is not allowed to contain 0. Given the upper and lower boundary numbers, output a list, the elements of the list are all self-divisors within the boundary (including the boundary).

Note:
The boundary of each input parameter satisfies 1 <= left <= right <= 10000.

Example 1:
Input:
upper boundary left = 1, lower boundary right = 22
Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15, 22]

code show as below:

class Solution {
    
    
    public static List<Integer> selfDividingNumbers(int left, int right) {
    
    
        boolean flag;
        List<Integer> arr = new ArrayList<>();
        for (int i = left; i <= right; i++) {
    
    
            flag = true;
            List<Integer> list = new ArrayList<>();
            list = weiShu(i);
            if (list.contains(0)) {
    
    
                continue;
            } else {
    
    
                int n = list.size();
                for (int j = 0; j < n; j++) {
    
    
                    if (i % list.get(j) != 0) {
    
    
                        flag = false;
                        break;
                    }
                }
                if (flag) {
    
    
                    arr.add(i);
                }
            }
        }
        return arr;
    }

    public static List<Integer> weiShu(int x) {
    
    
        List<Integer> list = new ArrayList<>();
        int m;
        while (x > 0) {
    
    
            m = x % 10;
            list.add(m);
            x /= 10;
        }
        return list;
    }
}

Results of the:
Insert picture description here

Guess you like

Origin blog.csdn.net/FYPPPP/article/details/114241254