class Solution {
public int countDigitOne(int n) {
if(n<1)
return 0;
int count = 0;
int base = 1;
int round = n;
while(round>0){
int weight = round%10;
round/=10;
count += round*base;
if(weight==1)
count+=(n%base)+1;
else if(weight>1)
count+=base;
base*=10;
}
return count;
}
}
本来通过暴力法做,但是时间太久了```
这是通过找规律做的,不理解的可以私信我。
为了方便你们理解,给你们个提示,个位增长10,十位加1 以此类推,
你们懂的。。。。。。
最后
以上就是顺心发箍最近收集整理的关于给定一个整数 n,计算所有小于等于 n 的非负整数中数字 1 出现的个数。的全部内容,更多相关给定一个整数内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复