http://www.lintcode.com/en/problem/digit-counts/
Enumeration, there are three cases.
The first case is that the current bit is less than K, at which point the count on that bit depends on the value above that bit;
In the second case, the current bit equals K, at which point the count on that bit depends on the bit above and below that bit;
In the third case, where the current bit is greater than K, then the count of this bit depends on the high
Better think, here is the code
classSolution { Public: /** param k:as description. * param n:as description. * Return:how many K ' s between 0 and N. */ intDigitcounts (intKintN) {//Write your code here Long Long Base=1; intres =0; while(Base<=N) { intCur = (N/Base) %Ten; intHigh = n/Base/Ten; intLow = nBase; if(cur > k) res + = (High +1) *Base; Else if(cur = = k) Res + = (Low +1) + High *Base; ElseRes + = high *Base; Base*=Ten; } returnRes; }};
Lintcode Digit Counts