Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.
样例 Given num = 38. The process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return 2.
首先:12345 = 1 * 9999 + 2 * 999 + 3 * 99 + 4 * 9
public class Solution {
/**
* @param num a non-negative integer
* @return one digit
*/
public int addDigits(int num) {
return (num - 1) % 9 + 1;
}
}