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;
}
};
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;
}
};
相关文章推荐
- java 数据类型转换的陷阱
- ios开发之 - xcode 快捷键
- 紫书第4章 函数和递归(部分题)
- poj 2446 Chessboard(经典二分匹配)
- 前端路上的各种问题-javascript-2
- test
- iOS学习总结之UI-IB
- java实现快速排序两种方法
- 给初学者之一:浅谈java及应用
- ASP.NET MVC Unobtrusive JavaScript 实现 onfocusout 验证, onfocusin 清除错误(转)
- Web Storage做为简单数据库来使用
- 进制及其转换
- Unity3D有限状态机(FSM)学习笔记【3】FSState类
- 解析 STM32 的启动过程
- Hadoop与Spark常用配置参数总结
- java
- 深入理解磁盘文件系统之inode
- css3弹性盒布局
- Linux计划任务
- 向鼠标右键菜单增加菜单项