Simple math solution 0ms in C++


  • -1

    It's a permutation problem, the following code is quite terse, but I believe you can figure it out. Good luck!

    class Solution 
    {
    public:
        int countDigitOne(int num) 
        {
            int sum = 0;
            for(long i = 1; i <= num; i *= 10)
            {
                int a = num/i;
                int b = num%i;
                sum += (a+8)/10*i+(a%10==1)*(b+1);
            }
            return sum;
        }
    };

Log in to reply
 

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