258. Add Digits

Given a non-negative integernum, repeatedly add all its digits until the result has only one digit.

For example:

Givennum = 38, the process is like:3 + 8 = 11,1 + 1 = 2. Since2has only one digit, return it.

Follow up:

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

 /// 这个follow up 有点贱, 不是编程逻辑问题 纯数学问题, return 1+ (n-1)%9 ;  就酱!!! 别问我为什么, 如果面试出这个问题, 这人有点贱, 呵呵呵。

正常解法很简单

258. Add Digits_第1张图片

你可能感兴趣的:(258. Add Digits)