@@ -0,0 +1,1274 @@
+/**%0A * Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to%0A * n.%0A *%0A * For example:%0A * Given n = 13, Return 6, because digit 1 occurred in the following numbers: 1, 10, 11, 12, 13.%0A *%0A * Hint:%0A *%0A * Beware of overflow.%0A *%0A * Tags: Math%0A * Similar Problems: (E) Factorial Trailing Zeroes%0A */%0Apublic class NumberOfDigitOne %7B%0A%0A    /**%0A     * e.g n = 3141592, m = 100, a = 31415, b = 92%0A     * hundreds-digit of n is 1 for 3142 times, each has a 100 long streak, (a / 10 * 100) + 100%0A     * when m = 1000, a = 3141, b = 592%0A     * thousands-digit of n is 1 for 315 times, each has a 1000 long streak%0A     * since the digit is 1, the very last streak isn't 100 but only 592 + 1 = 593 numbers, (a / 10 * 1000) + (b + 1)%0A     * so the case is different when current digit is 0 or 1 or %3E= 2%0A     * (a + 8) / 10 is the number of full streaks (if current digit is 0 or 1, there won't be a full streak)%0A     * a %25 10 == 1 tells whether to add a partial streak (add partial streak only if current digit is 1)%0A     */%0A    public int countDigitOne(int n) %7B%0A        int res = 0;%0A        for (long m = 1; m %3C= n; m *= 10)%0A            res += (n / m + 8) / 10 * m + (n / m %25 10 == 1 ? n %25 m + 1 : 0);%0A        return res;%0A    %7D%0A%7D%0A

add 233 Number of Digit One