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.
Answer:
我的:
比较好的答案:
思路:根据自己的想法做的是通过循环然后一次次取个位十位相加,但是网上搜的答案更简单,但是有点理解不能,规律有点看不太懂。
Answer:
我的:
public class Solution {
public int addDigits(int num) {
while(num>=10){
int geWei = num%10;
int shiWei = num/10;
num = geWei + shiWei;
}
return num;
}
}
比较好的答案:
public class Solution {
public int addDigits(int num) {
return (num-1)%9+1;
}
}
思路:根据自己的想法做的是通过循环然后一次次取个位十位相加,但是网上搜的答案更简单,但是有点理解不能,规律有点看不太懂。