问题描述:
Given an integer num, repeatedly add all its digits until the result has only one digit, and return it. 如果num不是一位数,则将其每一位的数字累加,直到最终的累加和为一位数。
i.e. 976->22->4
代码如下:
class Solution {
public int addDigits(int num) {
while(num>=10){
int local_sum=0;
while(num>0){
local_sum=local_sum+num%10;
num=num/10;
}
num=local_sum;
}
return num;
}
}
时间复杂度O(logn)