All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0701_0800.s0728_self_dividing_numbers.Solution Maven / Gradle / Ivy

There is a newer version: 1.35
Show newest version
package g0701_0800.s0728_self_dividing_numbers;

// #Easy #Math #2022_03_24_Time_1_ms_(100.00%)_Space_39.8_MB_(88.16%)

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

public class Solution {
    public List selfDividingNumbers(final int left, final int right) {
        final List list = new ArrayList<>();
        for (int i = left; i <= right; i++) {
            if (isSelfDividing(i)) {
                list.add(i);
            }
        }
        return list;
    }

    private boolean isSelfDividing(int value) {
        final int origin = value;
        while (value != 0) {
            final int digit = value % 10;
            value /= 10;
            if (digit == 0 || origin % digit != 0) {
                return false;
            }
        }
        return true;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy