题解 | #整数中1出现的次数(从1到n整数中1出现的次数)#
整数中1出现的次数(从1到n整数中1出现的次数)
http://www.nowcoder.com/practice/bd7f978302044eee894445e244c7eee6
public int NumberOf1Between1AndN_Solution(int n) {
int high = n/10;
int cur = n%10;
int d = 1;
int low = 0;
int count = 0;
while(cur != 0 || high != 0){
if(cur == 0){
count = count + high * d;
}else if(cur == 1){
count = count + high * d + low + 1;
}else{
count = count + (high+1)*d;
}
low = low + cur*d;
cur = high%10;
high = high/10;
d = d *10;
}
return count;
}
}