forked from haoel/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
addDigits.cpp
88 lines (76 loc) · 2.58 KB
/
addDigits.cpp
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Source : https://leetcode.com/problems/add-digits/
// Author : Timothy Lim, Hao Chen
// Date : 2015-10-1
/**********************************************************************************
* 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.
*
* Follow up:
* Could you do it without any loop/recursion in O(1) runtime?
*
**********************************************************************************/
class Solution {
public:
int addDigits(int num) {
switch(random()%5+1){
case 1: return addDigits01(num);
case 2: return addDigits02(num);
case 3: return addDigits03(num);
case 4: return addDigits04(num);
default: return addDigits05(num);
}
}
//regualr way
int addDigits01(int num) {
while(num > 9) {
int sum;
for(sum=0; num > 0; sum += num%10 , num/=10);
num = sum;
}
return num;
}
//This solution looks is very tricky, but acutally it is easy to understand.
//it just keep adding the last digital until the num < 10
int addDigits02(int num) {
while(num > 9) {
num = num / 10 + num % 10;
}
return num;
}
// Let's observe the pattern
// 1 1
// 2 2
// ... ...
// 8 8
// 9 9
// 10 1
// 11 2
// 12 3
// ... ...
// 17 8
// 18 9
// 19 1
// 20 2
// ... ...
// It looks most of number just simply %9 is the answer,
// but there are some edge cases.
// 9%9=0 but we need 9.
// 18%9=0 but we need 9
// so we can find the solution is:
// 1) num <=9, return num
// 2) num > 9, reutrn num%9 if num%9>0
// return 9 if num%9 ==0
int addDigits03(int num) {
return num >9 ? ((num %9)==0 ? 9:num%9) : num;
}
//But actually, we can use (num-1)%9 + 1 to make all cases right.
int addDigits04(int num){
return (num - 1) % 9 + 1;
}
//This solution is similar with pervious solution.
int addDigits05(int num){
return num - 9 * ((num - 1)/9);
}
};