-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDemo.java
More file actions
75 lines (67 loc) · 2.02 KB
/
Copy pathDemo.java
File metadata and controls
75 lines (67 loc) · 2.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
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
package test;
import java.util.ArrayList;
/**
* @Author: wei1
* @Date: Create in 2019/1/31 23:34
* @Description:
*/
public class Demo {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
String randomStr =
(int) (Math.random() * 10000) + "" + (int) (Math.random() * 1000000000);
// String randomStr = "11";
System.out.println(randomStr);
cutStr(randomStr);
}
}
//用5个分割符去切分字符串,每个字符串代表的数字要小于350,也可以为长度为0的字符串
public static void cutStr(String str) {
if (str == null) {
return;
}
if (isNotNum(str)) {
return;
}
dfs(str, 5, new ArrayList<String>(6));
}
private static boolean isNotNum(String str) {
char[] chars = str.toCharArray();
for (int i = 0; i < chars.length; i++) {
int x = chars[i] - '0';
if (x < 0 || x > 9) {
return true;
}
}
return false;
}
private static void dfs(String str, int i, ArrayList<String> temp) {
if (str.length() > (i + 1) * 3) {
return;
}
if (i == -1 && temp.size() == 6) {
System.out.println(temp);
return;
}
//下一个分割的字符串长度为0
for (int j = 0; j <= 3; j++) {
if (str.length() < j) {
return;
}
String substring = str.substring(0, j);
int num = 0;
if (j != 0) {
num = Integer.parseInt(substring);
if (num < 350) {
temp.add(substring);
dfs(str.substring(j), i - 1, temp);
temp.remove(temp.size() - 1);
}
} else {
temp.add(" ");
dfs(str.substring(j), i - 1, temp);
temp.remove(temp.size() - 1);
}
}
}
}