题解 | #整数中1出现的次数(从1到n中1出现的次数)#
整数中1出现的次数(从1到n整数中1出现的次数)
https://www.nowcoder.com/practice/bd7f978302044eee894445e244c7eee6
class Solution {
public:
int NumberOf1Between1AndN_Solution(int n) {
if (n == 0) {
return 0;
}
if (n < 10) {
return 1;
}
int count = 1;
for (int i = 10; i <= n; ++i) {
int temp = i;
while (temp > 0) {
if (temp % 10 == 1) {
count++;
}
temp /= 10;
}
}
return count;
}
};
