public class Solution {public int NumberOf1Between1AndN_Solution(int n) {
//根据当前位置为各位1 十位10 百位100等 来计算
//有如下规则,如果当前位为0,则1的个数仅有高位来计算=高位*当前位
//***************如果当前位为1,则=高位*当前位+低位+1
//************** 如果当前位为2-9:则=(高位+1)*当前位//i=1开始计算,每次前移一位*10 直到n/i=0停止
int current = 0;
int before = 0;
int after = 0;
int count = 0;
int i=1;
while(n/i != 0){
current = (n/i)%10; //计算当前位
before = n/(i*10); //高位
after = n - (n/i)*i; //低位
if(current==0)
count += before*i;
else if(current==1)
count = count + before*i + after + 1;
else if(current > 1)
count = count + (before+1)*i;
i=i*10;
}
return count;
}
}
****重点在于计算1的个数的方法以及计算当前位和高位低位的方法****