LeetCode258题:各位相加——数字根

LeetCode258题:各位相加——数字根_第1张图片

这道题涉及到数学领域的一个定理,即“数字根”。

 

解法一:笨方法(循环)

public int addDigits(int num) {
        Integer res = new Integer(num);
        while(res>9){
            String str = res.toString();
            int temp = 0;
            for(int i=0;i

解法二:公式法

public int addDigits(int num) {
        return (num-1)%9+1;
    }

 

你可能感兴趣的:(算法,LeetCode,LeetCode,258,数字根,数根)