输入一个整数 n ,求1~n这n个整数的十进制表示中1出现的次数
例如,1~13中包含1的数字有1、10、11、12、13因此共出现6次
public class Solution {
public int NumberOf1Between1AndN_Solution(int n) {
int count = 0;
for (int i = 1; i <= n ; i*= 10){
int high = n / (i * 10);
int low = n % i;
int cur = n / i % 10;
if(cur == 0){
count += high * i;
}else if(cur == 1){
count += high * i + (low + 1);
} else {
count += (high + 1) * i;
}
}
return count;
}
}