-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddDigits.java
More file actions
45 lines (41 loc) · 1.02 KB
/
AddDigits.java
File metadata and controls
45 lines (41 loc) · 1.02 KB
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
package com.leetcode;
/**
* Created by jamylu on 2018/3/4.
* leetcode258
* 将一个数的每一位相加,依次往下,直至这个数变成一位
*/
public class AddDigits {
public static void main(String[] args) {
int num = 38;
System.out.println(addDigits(num));
System.out.println(add(num));
}
// 暴力法可以通过
public static int addDigits(int num) {
int tmp = 0;
if (num <= 9) {
return num;
} else {
while (num > 9) {
while (num > 0) {
tmp += num % 10;
num /= 10;
}
num = tmp;
tmp = 0;
}
return num;
}
}
// 简单解法,巧妙利用9的倍数的各位数相加仍然是9的倍数
public static int add(int num) {
if (num == 0) {
return 0;
}
if (num % 9 == 0) {
return 9;
} else {
return num % 9;
}
}
}