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;  
    }  
}  

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容