C++ Code easy to understand


  • 0
    P

    class Solution {
    public:
    vector<int> selfDividingNumbers(int left, int right) {

        vector<int> final_ans;
        for(int i = left; i <= right; i++){
            int temp = i;
            int count=0, n = 0;
            int base = 10;
            // Calculating the number of digits in number
            while(temp){
                n++;
                temp /= 10;
            }
            
                temp = i;
                int num = 0;
                // Dividing each number by every digits that number contains 
                // and increasing count, if count == number of digits means each 
                // each digits divides the number, so push back to vector
                while(temp){
                    num = temp%10;
                    if(num != 0 && i % num == 0){
                       count++; 
                    }
                    temp /= 10;
                }
            
            if(n == count){
               final_ans.push_back(i); 
            }
            
        }
        
        return final_ans;
    }
    

    };


Log in to reply
 

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