Given a non-negative integer num
, repeatedly add all its digits until the result has only one digit.
Example:
Input:38
Output: 2 Explanation: 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?
X=fn*Xn+fn-1*Xn-1+...+f1*x1 , Xn=pow(10,n-1); Every operator make X=> X'=fn+fn-1+...f1 X-X' =fn*(Xn - 1)+fn-1*(Xn-1 - 1)+...f1*(X1 - 1) =fn*9···99+fn-1*9···9+..f1*0 =9*(fn*1···11+fn-1*1···1+...f2*1+f1*0) =9*S (S is a non-negative integer) => Everytime reduce a number of multiple 9
class Solution {
public:
int addDigits(int num) {
if(num==0)
return 0;
int t=num%9;
return (t!=0)?t:9;
}
};