O ms solution in C++


  • 0
    Y
    class Solution {
    public:
        int count_digit(int n)
        {
            int count=0;
            while(n!=0)
            {
                count++;
                n/=10;
            }
            return count;
        }
        
        int countDigitOne(int n) {
            if(n<=0)
            {
                return 0;
            }
            long long number = n;
            long long digit;
            digit = count_digit(number);
            if(digit==1)
            {
                return 1;
            }
            cout<<digit<<'\n';
            if(digit==0)
            {
                return 0;
            }
            long long result=(long long)(pow(10,digit-1))*(digit-1)/10;
            long long initial = number/(long long)(pow(10,digit-1));
            long long rem = number%(long long)(pow(10,digit-1));
            long long left=countDigitOne(rem);
            result*=initial;
            if(initial==1)
            {
                result+=left;
                result+=(rem+1);
            }
            else
            {
                result+=left;
                result+=(long long)(pow(10,digit-1));
            }
            return result;
            
        }
    };

Log in to reply
 

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