LeetCode258:Add Digits
2016-03-02 20:36
239 查看
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
2has only one digit, return it.
Follow up:
Could you do it without any loop/recursion in O(1) runtime?
Hint:
- A naive implementation of the above process is trivial. Could you come up with other methods?
- What are all the possible results?
- How do they occur, periodically or randomly?
- You may find this Wikipedia article useful.
int addDigits(int num) { return num-(num-1)/9*9; }
相关文章推荐
- 另一个前端优化
- JS原型和闭包
- UVA - 12230 Crossing Rivers (期望)
- ios autolayout中NSLayoutAttributeLeft(Right)与NSLayoutAttributeLeading(Trailing)区别
- Easyui---模态对话框实现ESC键一键锁屏(输入密码解锁)
- 【C#】基础知识—数据类型、数据类型转换
- vs2010开发activex(MFC)控件/ie插件(一)
- 网站前端优化
- hdu2562
- 将项目部署到Tomcat7时遇到的一点小问题
- 最长递增子序列问题 2011-12-29
- hdu Rescue (bfs)
- 放大镜效果实现
- 在嵌入式设备中使用protobuf
- vs生成的exe程序和相关dll打包
- iOS中的 多线程- NSThread 与 NSObject
- 链表的部分翻转
- CF Zepto Code Rush 2014 B. Om Nom and Spiders
- 字符串
- UI-进程与线程(多线程、主次线程)