-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddDigits.java
More file actions
38 lines (34 loc) · 916 Bytes
/
AddDigits.java
File metadata and controls
38 lines (34 loc) · 916 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package easy;
/**
*
* ClassName: AddDigits
* @author chenyiAlone
* Create Time: 2019/03/06 21:24:19
* Description: No.258
* Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.
Example:
Input: 38
Output: 2
Explanation: The process is like: 3 + 8 = 11, 1 + 1 = 2.
Since 2 has only one digit, return it.
*/
public class AddDigits {
public int addDigits(int num) {
while (num > 9) {
num = helper(num);
}
return num;
}
public static int helper(int num) {
int res = 0;
while (num > 0) {
res += num % 10;
num /= 10;
}
return res;
}
public static void main(String[] args) {
int num =38;
System.out.println(new AddDigits().addDigits(num));
}
}