您的位置:首页 > 其它

leetcode 233: Number of Digit One

2015-08-30 20:29 423 查看
This problem is a little hard to understand. You can write some numbers and calculate the number of Digit Ones by hand to find the law. 

I learned the method from http://www.07net01.com/2015/07/886667.html.

And the code is from http://blog.csdn.net/dd523762588/article/details/46816133.

class Solution {
public:
int countDigitOne(int n) {
int res=0;
int factor=1,rest=0;
while(n>0)
{
int r=n%10;
n/=10;
if(r==0)
res+=n*factor;
else if(r==1)
res+=n*factor+rest+1;
else
res+=(n+1)*factor;
rest+=r*factor;
factor*=10;
}
return res;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: