258. Add Digits

2016-12-02  本文已影响0人  我是你的果果呀

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 ;  就酱!!! 别问我为什么, 如果面试出这个问题, 这人有点贱, 呵呵呵。

正常解法很简单

上一篇下一篇

猜你喜欢

热点阅读