my 3ms C solution,both use loop and recursion...


  • 0
    S
    int recur(n){
    	return (n / 10>0) ? recur(n / 10) + n % 10 : n % 10;
    }
    
    int addDigits(int num) {
    	int tmp = num;
    	while (tmp / 10){
    		tmp = recur(tmp);
    	}
    	return tmp;
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.