leetcode NumberOfDigitOne
z
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class NumberOfDigitOne {
/**
* Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n.
*
* Example:
*
* Input: 13
* Output: 6
* Explanation: Digit 1 occurred in the following numbers: 1, 10, 11, 12, 13.
*
*/
public static int countDigitOne(int n) {
int count = 0;
for (int i = 0; i<=n; i=i*10){
int p = n/i;
int q = n%i;
count += p*10 ;
}
return count;
}
public static void main(String[] argg){
System.out.println(countDigitOne(91));
}
}