My simple java solution use loop and without loop


  • 4
    C
        public int addDigits(int num) {
    // use the hint dr(n)= n- 9*floor((n-1)/9)
        return num-9*((num-1)/9);
    }
    
    
    	public int addDigits(int num){
    	int result = 0;
    	if (num/10 <1) return num;
    	while(num/10 >= 1){
    		result += num % 10;
    		num = num/10;
    	}
    	result += num;
    	return addDigits(result);
    }

Log in to reply
 

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