Java Very Short Recursive Solution


  • 0
        public int countDigitOne(int n) { 
            if (n<=0) return 0;
            int multiplier = 1;
            while (multiplier <= n/10) multiplier *= 10;
            if (multiplier == 1) return 1;
            int mod = n%multiplier;
            int div = n/multiplier;
            int add = div == 1 ? mod + 1 : multiplier;
            return add + div*countDigitOne(multiplier-1) + countDigitOne(mod);
        }
    

Log in to reply
 

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