【leetcode】Add Digits[easy]


总要给自己一些惊喜和成就感,刷水题就是。

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 = 111 + 1 = 2. Since 2 has only one digit, return it.


递归:

public class Solution {
    public int addDigits(int num) {
         if(num <10)
        	return num;
        int yu = num % 10;
        int shang = num / 10;
        if(yu + shang < 10)
        	return yu + shang;
        return addDigits(yu + shang);
    }
}




你可能感兴趣的:(【leetcode】Add Digits[easy])