-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththeOne.java
More file actions
79 lines (61 loc) · 1.5 KB
/
Copy paththeOne.java
File metadata and controls
79 lines (61 loc) · 1.5 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
76
77
78
79
package test;
import java.util.HashSet;
public class theOne {
public static long[] total = new long[1];
static long newNum = 0;
public void getPairs(int A, int B) {
long totalPair = 0;
HashSet<Long> set = new HashSet<Long>();
for (int i = A; i <= B; i++) {
total[0] = 0;
set.clear();
char[] charArr = changeToCharArr(i);
arrange(charArr, 0, charArr.length - 1, total, set, i, B);
totalPair += total[0];
}
System.out.print(totalPair);
}
public static void arrange(char[] str, int st, int len, long[] total,
HashSet<Long> set, int A, int B) {
String newStr = "";
if (st == len) {
for (int i = 0; i <= len; i++) {
newStr += str[i];
if (newStr.length() == len + 1) {
newNum = Integer.parseInt(newStr);
if (newNum > A && newNum < B) {
if (!set.contains(newNum)) {
total[0]++;
set.add(newNum);
System.out.print(A + "->");
System.out.println(newNum);
//System.out.println(".");
}
}
}
}
return;
} else {
for (int i = st; i <= len; i++) {
swap(str, st, i);
arrange(str, st + 1, len, total, set, A, B);
swap(str, st, i);
}
}
return;
}
public static void swap(char[] str, int i, int j) {
char temp = str[i];
str[i] = str[j];
str[j] = temp;
}
public static char[] changeToCharArr(int A) {
String s = Integer.toString(A);
char[] carr = s.toCharArray();
return carr;
}
public static void main(String[] args) {
theOne sp = new theOne();
sp.getPairs(100, 999);
}
}