Digit Counts 算法
Count the number of k between 0 and n. k can be 0 - 9.
public class Solution {
/**
* @param k: An integer
* @param n: An integer
* @return: An integer denote the count of digit k in 1..n
*/
public int digitCounts(int k, int n) {
// write your code here
int count = 0 ;
// int[] c = new int[n] ;
//for (int i = 0 ; i <= n ; i++ ){
//c[i] = i ;
// }
int c = 0 ;
for( int i = 0 ; i <= n ; i++){
c = i ;
if (c == k){
count++ ;
}else {
while(c >= 10){
if (k == c%10 && c/10 != 0 ){
count++ ;
}
if( k==c/10 && c/10 != 0){
count++ ;
}
c = c/10 ;
}
}
}
return count ;
}
}