My C# solution.


  • 0
    M
    public class Solution {
        public IList<int> SelfDividingNumbers(int left, int right) {
            IList<int> result = new List<int>();
            
            for(int i = left; i <= right; i++)
            {
                //less than 10
                if(Math.Abs(i) < 10)
                {
                    result.Add(i);
                }
                else if( i % 10 == 0) //last digit equals 0
                {
                    continue;
                }
                else
                {
                    int value = i;
                    while(value > 0)
                    {
                        if( value % 10 == 0)
                        {
                            break;
                        }
                        else if( i % (value % 10) != 0)
                        {
                            break;
                        }
                        value = value / 10;
                    }
                    if( value == 0)
                    {
                        result.Add(i);
                    }           
                }
            }
            return result;
        }
    }
    

Log in to reply
 

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