forked from geekxh/hello-algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.java
75 lines (68 loc) · 1.54 KB
/
Solution.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
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
/**
* @author Anonymous
* @since 2019/11/20
*/
public class Solution {
/**
* 打印从1到最大的n位数
*
* @param n n位
*/
public void print1ToMaxOfNDigits(int n) {
if (n < 1) {
return;
}
char[] chars = new char[n];
for (int i = 0; i < n; ++i) {
chars[i] = '0';
}
while (!increment(chars)) {
printNumber(chars);
}
}
/**
* 打印数字(去除前面的0)
*
* @param chars 数字数组
*/
private void printNumber(char[] chars) {
int index = 0;
int n = chars.length;
for (char ch : chars) {
if (ch != '0') {
break;
}
++index;
}
StringBuilder sb = new StringBuilder();
for (int i = index; i < n; ++i) {
sb.append(chars[i]);
}
System.out.println(sb.toString());
}
/**
* 数字加1
*
* @param chars 数字数组
* @return 是否溢出
*/
private boolean increment(char[] chars) {
boolean flag = false;
int n = chars.length;
int carry = 1;
for (int i = n - 1; i >= 0; --i) {
int num = chars[i] - '0' + carry;
if (num > 9) {
if (i == 0) {
flag = true;
break;
}
chars[i] = '0';
} else {
++chars[i];
break;
}
}
return flag;
}
}