I found a answer which is very cool!


  • -19

    //This is other's answer:
    public static int addDigits(int num) {
    int result = Test.addNum(num);
    while(result > 9){
    result = Test.addNum(result);
    }

    	return result;
    }
    

    //below is mine:
    public static int addDigits(int num) {
    int result = Test.addNum(num);
    while(result > 9){
    result = Test.addNum(result);
    }

    	return result;
    }
    public static int addNum(int num){
    	int result = 0;
    	while(num != 0){
    		result += (num%10);
    		num /= 10;
    	}
    	return result;
    }

Log in to reply
 

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