My recursion implementation


  • 5
    M
    int countDigitOne(int n) {
       if(n <= 0)
            return 0;
    
    
       int j = 1;
       int k = n ;
       int constant = 0;
       while(k >= 10)
       {
           k = k/10;
           j = j *10;
       }
       if( k > 1)
       {
           constant = j;
       }
       else
       {
           constant = n%j+1;
       }
       return constant + k*countDigitOne(j-1)+countDigitOne(n%j);
    }

  • 0
    S

    Amazing solution! Would you please explain a bit about your thought? Thanks.


  • 0
    S

    No need to bother then, now I get it...
    Thank you again


Log in to reply
 

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