题解 | #单链表的排序#
整数中1出现的次数(从1到n整数中1出现的次数)
http://www.nowcoder.com/practice/bd7f978302044eee894445e244c7eee6
public class Solution {
public int NumberOf1Between1AndN_Solution(int n) {
int digit = 1;
int cur = n % 10;
int high = n / 10;
int low = 0;
int count = 0;
while (n != low) {
if (cur == 0) {
count += (high * digit);
} else if (cur == 1) {
count += (high * digit + low + 1);
} else {
count += ((high + 1) * digit);
}
low += cur * digit;
cur = high % 10;
high = high / 10;
digit *= 10;
}
return count;
}
}
查看14道真题和解析