Why does my recursive solution get this strange answer?


  • 0
    F
    class Solution {
    public:
        int countDigitOne(int n) {
            if (n<1) return 0;
            if (n<10) return 1;
            int m = n;
            int cnt = 0;
            while(m/10) {
                m/=10;
                cnt ++;
            }
            if (1 == m) {
                return  countDigitOne(pow(10,cnt)-1) + n-pow(10,cnt)+1 + countDigitOne(n-pow(10,cnt));
            }
            else {
                return pow(10,cnt)+(m)*countDigitOne(pow(10,cnt)-1) + countDigitOne(n-m*pow(10,cnt));
            }
            return 0;
        }
    };
    
    
    
    And when run against the following test case, it gets...
    Input:
    1410065408
    Output:
    -2147483648
    Expected:
    1737167499
    

    Just cannot understand...


  • 0
    E

    int to long long


Log in to reply
 

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