Beats 95% of solutions. Easiest to understand.


  • 0
    D

    Brute Force approach
    Time: 5ms

    public List<Integer> selfDividingNumbers(int left, int right)
    {
        List<Integer> list = new ArrayList<Integer>();
        for(int i=left;i<=right;i++)
        {
            if(isSelfDiving(i))
            {
                list.add(i);
            }
        } 
        return list;
    }
    
    public boolean isSelfDiving(int num)
    {
        int val = num;
        while(val!=0)
        {
            int rem = val%10;
            if(rem==0) return false;           
            if(num%rem!=0)
                return false;
            val = val/10;
        }
        return true;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.