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.
public class Solution {
public int addDigits(int num) {
return (num - 1) % 9 + 1;
}
}
本文介绍了一个用于将非负整数连续相加直至得到一位数的算法实现,通过实例演示了如何计算给定整数直到其结果为一位数的过程。
1291

被折叠的 条评论
为什么被折叠?



