-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathAddDigits.java
41 lines (34 loc) · 875 Bytes
/
AddDigits.java
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
39
40
41
/*
Given a non-negative integer num, repeatedly add all its digits until the
result has only one digit.
Example
Given num = 38.
The process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return
2.
Challenge
Could you do it without any loop/recursion in O(1) runtime?
*/
public class AddDigits {
/**
* @param num a non-negative integer
* @return one digit
*/
// O(1) time.
public int addDigits(int num) {
return (num - 1) % 9 + 1;
}
/*****************************************************************************/
public int addDigits(int num) {
while (num >= 10) {
num = sumOfDigits(num);
}
return num;
}
public int sumOfDigits(int num) {
int sum = 0;
for (; num != 0; num /= 10) {
sum += num % 10;
}
return sum;
}
}