258. Add Digits
2016-03-13 20:30
239 查看
Given a non-negative integer
one digit.
For example:
Given
only one digit, return it.
Follow up:
Could you do it without any loop/recursion in O(1) runtime?
这个很简单了。与9同余,关键就是如果n不是0的话得到的余数不能为0而是为9
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?
这个很简单了。与9同余,关键就是如果n不是0的话得到的余数不能为0而是为9
public class Solution { public int addDigits(int n) { return (n-1)%9+1; } }
相关文章推荐
- struts2从action向jsp传参数
- 数据库设计(机房收费系统)
- POJ1947 Rebuilding Roads(树形DP)
- 第三周学习笔记
- php中cookie与session关系
- 三秒钟修改网页内容
- List<T>和Ilist<T>的区别
- 为什么阿法狗不敢挑战麻将?
- maven下载与安装配置
- B - Making a String-----CodeForces 624B
- 为什么阿法狗不敢挑战麻将?
- HBase的特点
- 复利计算程序说明
- Python之Django-part 1
- 稀疏矩阵的压缩存储及快速转置
- 线段树(区间修改、区间查询) HDU 1754 I Hate It
- [writeup]360-ctf-2014-re123
- Eclipse快速添加get、set方法
- html中文乱码问题的解决
- MRC的开发原则