//归纳当前为cur为不同值时当前为1的个数的规律
public class 整数1出现的次数优解 {
public static void main(String[] args) {
int count = new 整数1出现的次数优解().NumberOf1Between1AndN_Solution(13);
System.out.println(count);
}
public int NumberOf1Between1AndN_Solution(int n) {
int count = 0;
int digit = 1;
int high = n /10;
int cur = n % 10;
int low = 0;
while (cur!=0 || high!=0){
if(cur == 0){
count += high*digit;
}else if(cur == 1){
count += high*digit+low+1;
}else {
count +=(high+1)*digit;
}
low = low + cur * digit;
cur = high % 10;
high /= 10;
digit *= 10;
}
return count;
}
}
原文:https://www.cnblogs.com/chyEric/p/14395404.html