258. Add Digits
来源:互联网 发布:数据统计问题有哪些 编辑:程序博客网 时间:2024/05/28 15:06
258. Add Digits
DescriptionHintsSubmissionsDiscussSolution
DiscussPick One
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?
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
题意:
给定一个整数,求其各位之和,直到只剩下一位数
算法思路:
循环相加直到小于10即可
代码:
package easy;/*@author wchstrife@version 2017年7月22日下午11:03:12*/public class AddDigits {public int addDigits(int num) {while(num >9){num = num/10 + num%10;}return num;}}
阅读全文
0 0
- 258.Add Digits
- 【LeetCode】258.Add Digits
- 258. Add Digits
- LeetCode 258. Add Digits
- 258. Add Digits
- LeetCode : 258. Add Digits
- 258. Add Digits
- 258. Add Digits
- 258. Add Digits
- 258. Add Digits
- 258. Add Digits
- leetCode 258. Add Digits
- LeetCode 258. Add Digits
- 258. Add Digits LeetCode
- 258. Add Digits
- 258. Add Digits
- LeetCode 258. Add Digits
- 258. Add Digits
- 刷题——surrounded-regions
- Hibernate-Helloworld入门实例
- 【bzoj2453】 维护队列
- 强连通分量板子 hdu 3072
- HIbernate第二课--基本注解,主键生成策略
- 258. Add Digits
- Hibernate第三课--级联关系,关联关系,集合和继承映射
- 并查集简单实现
- 02.面向对象编程
- Hibernate第四课--HIbernate三大状态转换
- Linux常用命令
- Hibernate第五课--查询
- 初学java的String类的compareTo方法
- MVG读书笔记——几何变换