LeetCode No.258 Add Digits | #digital root

Q:

Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. For example: Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it.

A:

Digital root,针对的是".... process continues until a single-digit number is reached.”
digital root is the value module 9. 证明采取mod9的方法有很多。

public class Solution  {  
    public int addDigits(int num)  {  
        if (0 == num) return 0;  
        else if (num % 9 == 0) return 9;  
        else return num % 9;  
    }  
}  
LeetCode No.258 Add Digits | #digital root_第1张图片

不知道为什么,看这道题,脑子里总是想到之前automata theory课上学到的东西。

你可能感兴趣的:(LeetCode No.258 Add Digits | #digital root)