leetcode
2015-09-22 10:54
357 查看
Add Digits:
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.
For example:
Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it.
Follow up:
Could you do it without any loop/recursion in O(1) runtime?
class Solution {
public:
int addDigits(int num) {
int divideNum = 0;
if( num < 10)
{
return num;
}else
{
while( num )
{
divideNum += num % 10;
num = num / 10;
}
addDigits( divideNum );
}
};
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.
For example:
Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it.
Follow up:
Could you do it without any loop/recursion in O(1) runtime?
class Solution {
public:
int addDigits(int num) {
int divideNum = 0;
if( num < 10)
{
return num;
}else
{
while( num )
{
divideNum += num % 10;
num = num / 10;
}
addDigits( divideNum );
}
}
};
相关文章推荐
- XSS攻击及防御
- div纵向居中的方法(转载)
- 正则表达式截取字符串的方法技巧
- spring-MVC配置问题
- 6 - 对象和类
- 虚拟机检测绕过总结--不定时更新
- 预编译头文件来自编译器的早期版本,或者预编译头为 C++ 而在 C 中使用它(或相反)
- DSST(Accurate Scale Estimation for Robust Visual Tracking 代码解读
- atitit..主流 浏览器 js 发动机 内核 市场份额 attialx总结vOa9
- Javascript Array.prototype.some()
- (转)经典SQL查询语句大全
- c语言中宏定义的用法
- Hibernate分页查询小结
- android ndk socket()系统调用
- iOS9 beta 请求出现App Transport Security has blocked a cleartext HTTP (http://)
- Web攻防之XSS,CSRF,SQL注入
- 几个重要的jvm参数配置及建议
- document.referrer 获取来路url和当前url地址
- C#控制管理VisualSVN Server
- 自定义控件(视图)28期笔记01:自定义控件之学习思路