import java.util.ArrayList;
import java.util.List;

// 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.

/**
 * score:35.92(8ms)
 * time:O(nlogn)
 * space:O(1)
 */
class Solution {
    public List<Integer> selfDividingNumbers(int left, int right) {
        List<Integer> ret = new ArrayList<>();
        for (int i = left; i <= right; i++) {
            int remainder = i % 10;
            int copyI = i;
            while (copyI > 0 && remainder !=0 && i % remainder == 0) {
                copyI = (int) Math.floor(copyI / 10);
                remainder = copyI % 10;

                if (copyI == 0) {
                    ret.add(i);
                }
            }
        }

        return ret;
    }
}