# Leetcode no. 258

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?

Hint:

1. A naive implementation of the above process is trivial. Could you come up with other methods?
2. What are all the possible results?
3. How do they occur, periodically or randomly?
4. You may find this Wikipedia article useful.

public class Solution {
if (num==0) return 0;
return (num % 9==0) ? 9 : (num % 9);
}
}

#### leetcode258题 题解 翻译 C语言版 Python版

2016-02-03 17:27:59

2015-08-25 13:18:16

2016-04-09 05:47:27

2016-04-19 09:35:13

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

2015-11-21 20:15:53

#### [leetcode]Python实现-258.各位相加

2018-06-13 15:55:40

#### leetcode【217+219 Contains Duplicate I+II】【python】

2016-09-08 10:33:49

2015-12-24 10:17:35

#### Leetcode 258 258

2015-08-27 11:11:50

#### Leetcode—Contains Duplicate—Python的三种写法

2015-06-17 20:01:58