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.
Follow up:
Could you do it without any loop/recursion in O(1) runtime?
First, I assumed that I should transfer a string into char, and then did something. So I read this blog.
http://blog.csdn.net/kankan_summer/article/details/7243493
But, we should do it without any loop/recursion in O(1) runtime. I did not know how to do, so I found these solutions.
http://blog.csdn.net/xudli/article/details/47786855
http://my.oschina.net/Tsybius2014/blog/497645
http://blog.csdn.net/booirror/article/details/47710779
http://my.oschina.net/Tsybius2014/blog/497645