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?
1 public int addDigits(
int num)
2 {
3 if (num/10==0)
return num;
4 int sum = 0
;
5 while(num!=0
)
6 {
7 sum= sum+num
;
8 num= num/10
;
9 }
10 return addDigits(sum);
11
12 }
转载于:https://www.cnblogs.com/hygeia/p/4799858.html
相关资源:JAVA上百实例源码以及开源项目