package com.fishercoder.solutions;

/**
 * Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n.

 For example:
 Given n = 13,
 Return 6, because digit 1 occurred in the following numbers: 1, 10, 11, 12, 13.

 Hint:

 Beware of overflow.
 */
public class _233 {

    public int countDigitOne(int n) {
        int count = 0;
        for (long k = 1; k <= n; k *= 10) {
            long r = n / k;
            long m = n % k;
            // sum up the count of ones on every place k
            count += (r + 8) / 10 * k + (r % 10 == 1 ? m + 1 : 0);
        }
        return count;
    }

}

@@ -33,16 +33,47 @@
 s;%0A%0A/**%0A
+ * 233. Number of Digit One%0A *%0A
  * Given
@@ -303,18 +303,16 @@
 %0A%0A Hint:
-%0A%0A
  Beware 
@@ -348,21 +348,60 @@
  _233 %7B%0A
-%0A
+    public static class Solution1 %7B%0A   
     
+ 
 public i
@@ -434,16 +434,20 @@
         
+    
 int coun
@@ -461,16 +461,20 @@
         
+  
+  
 for (lon
@@ -509,24 +509,28 @@
             
+    
 long r = n /
@@ -541,24 +541,28 @@
             
+ 
+   
 long m = n %25
@@ -561,24 +561,28 @@
  m = n %25 k;%0A
+    
             
@@ -638,16 +638,20 @@
         
+    
 count +=
@@ -705,18 +705,26 @@
         
+   
+ 
 %7D%0A
+    
         
@@ -741,13 +741,22 @@
 nt;%0A    
+    %7D%0A    
 %7D
-%0A
 %0A%7D%0A

refactor 233