###### 第二次做leetcode中的难度为easy的题-258

Total Accepted: 45239 Total Submissions: 95297 Difficulty: Easy
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.
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.https://en.wikipedia.org/wiki/Digital_root

public static int DigitalRoot(int n){
int m=0;
while(n!=0){
m=m+n%10;
n=n/10;
}
if (m<10){
return m;
}else{
return DigitalRoot(m);//递归
}

}

1.一个数的数根加9等于这个数的数根；

2.一个数乘以9之后的数根为9；（说明如果一个数是9的倍数，那么它的根为9）

3.$\mathit{dr}(a+b) \equiv \mathit{dr}(\mathit{dr}(a)+\mathit{dr}(b) ).$

4.$\mathit{dr}(a \times b) \equiv \mathit{dr}(\mathit{dr}(a)\times\mathit{dr}(b) ).$

public class Solution {
public int addDigits(int n) {
if(n%9>0||n==0){ //注意边界
return n%9;
}else{
return 9;
}
}
}

public class Solution {
public int addDigits(int n) {
return n-9*((n-1)/9);
}
}

#### Leetcode难度表及解题汇总

2015-05-08 22:08:21

#### Leetcode解题思路总结(Easy)

2015-12-17 17:12:03

#### Leetcode题目分类：类型+难易

2015-04-15 16:16:48

#### LeetCode Easy排序前50道题

2018-01-08 19:59:12

#### LeetCode刷题记录Easy篇（1）

2017-04-15 21:44:12

#### leetcode刷题，总结，记录，备忘 201

2016-04-24 23:45:59

#### leetcode上面难度hard题

2017-05-15 21:38:49

#### Leetcode刷题汇总（Easy篇） -- Python

2018-01-17 23:34:32

#### LeetCode刷题（C++）——Implement strStr()（Easy）

2017-05-07 15:34:48

#### LeetCode高频题目（100）汇总-Java实现

2017-08-23 19:03:06