###### LeetCode 之 Add Digits

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 = 111 + 1 = 2. Since 2 has only one digit, return it.

Could you do it without any loop/recursion in O(1) runtime?

LeetCode前面的题都比较简单，有几个是关于数字规律的，如果能找到的话，很快就能解决，难度不在代码上的。最基本的方法是列举法从0到30我们就能很确定规律了：ans是以9为周期的，所以代码如下：

class Solution {
public:
int addDigits(int num) {
return (num-1)%9+1;
}
};

2016-05-08 22:39:54

#### [LeetCode 258] Add Digits

2015-08-25 13:18:16

#### LeetCode 258. Add Digits（数位相加）

2016-04-09 05:47:27

#### 258. Add Digits [easy] (Python)

2016-05-11 20:59:28

2016-01-17 13:21:58

#### [LeetCode 282] Expression Add Operator

2015-10-09 13:31:54

2016-04-19 09:35:13

#### [leetcode] 357. Count Numbers with Unique Digits 解题报告

2016-06-13 12:34:24

#### [leetcode] 402. Remove K Digits 解题报告

2016-09-19 11:50:18

#### Leetcode 402. Remove K Digits 删除K个数字 解题报告

2016-09-18 18:54:52

## 不良信息举报

LeetCode 之 Add Digits