感觉像是模拟题,
法1,暴力遍历,时间复杂度应该是On
class Solution {
public:
int get1(int x){
int sum = 0;
while(x){
if(x % 10 == 1){
sum++;
}
x = x / 10;
}
return sum;
}
int NumberOf1Between1AndN_Solution(int n) {
int res = 0;
for(int i = 1; i<=n; ++i){
res += get1(i);
}
return res;
}
};
方法2,参考No31、整数中1出现的次数 | 阿秀的学习笔记,不是很理解,有空再刷吧。